Capitalize the first and last character of each word in a string | JavaScript program to capitalise the first and last character of each word in a string . ; Create an equivalent string of the given string ; k stores index of first character and i is going to store index of last character . ; Check if the character is a small letter If yes , then Capitalise ; Driver code
function FirstAndLast ( str ) { var ch = str . split ( ' ' ) ; for ( var i = 0 ; i < ch . length ; i ++ ) { var k = i ; while ( i < ch . length && ch [ i ] != ' ' ) i ++ ; ch [ k ] = String . fromCharCode ( ch [ k ] >= ' ' && ch [ k ] <= ' ' ? ( ch [ k ] . charCodeAt ( 0 ) - 32 ) : ch [ k ] . charCodeAt ( 0 ) ) ; ch [ i - 1 ] = String . fromCharCode ( ch [ i - 1 ] >= ' ' && ch [ i - 1 ] <= ' ' ? ( ch [ i - 1 ] . charCodeAt ( 0 ) - 32 ) : ch [ i - 1 ] . charCodeAt ( 0 ) ) ; } return ch . join ( ' ' ) ; } var str = " " ; document . write ( str + " " ) ; document . write ( FirstAndLast ( str ) ) ;