Capitalize the first and last character of each word in a string | PHP 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
< ? php function FirstAndLast ( $ str ) { $ ch = $ str ; for ( $ i = 0 ; $ i < strlen ( $ ch ) ; $ i ++ ) { $ k = $ i ; while ( $ i < strlen ( $ ch ) && $ ch [ $ i ] != ' ▁ ' ) $ i ++ ; $ ch [ $ k ] = chr ( ( $ ch [ $ k ] >= ' a ' && $ ch [ $ k ] <= ' z ' ) ? ( ord ( $ ch [ $ k ] ) - 32 ) : ( ord ( $ ch [ $ k ] ) ) ) ; $ ch [ $ i - 1 ] = chr ( ( $ ch [ $ i - 1 ] >= ' a ' && $ ch [ $ i - 1 ] <= ' z ' ) ? ( ord ( $ ch [ $ i - 1 ] ) - 32 ) : ( ord ( $ ch [ $ i - 1 ] ) ) ) ; } return $ ch ; } $ str = " Geeks ▁ for ▁ Geeks " ; echo $ str , " STRNEWLINE " ; echo FirstAndLast ( $ str ) ; ? >