Print last character of each word in a string | Function to print the last character of each word in the given string ; Now , last word is also followed by a space ; If current character is a space ; Then previous character must be the last character of some word ; Driver code
function printLastChar ( str ) { str = str + " " ; for ( var i = 1 ; i < str . length ; i ++ ) { if ( str [ i ] === " " ) document . write ( str [ i - 1 ] + " " ) ; } } var str = " " ; printLastChar ( str ) ;