Print the first and last character of each word in a String | Function to print the first and last character of each word . ; If it is the first word of the string then print it . ; If it is the last word of the string then also print it . ; If there is a space print the successor and predecessor to space . ; Driver code
function FirstAndLast ( str ) { for ( var i = 0 ; i < str . length ; i ++ ) { if ( i == 0 ) document . write ( str [ i ] ) ; if ( i == str . length - 1 ) document . write ( str [ i ] ) ; if ( str [ i ] === " " ) { document . write ( str [ i - 1 ] + " " + str [ i + 1 ] ) ; } } } var str = " " ; FirstAndLast ( str ) ;