Print last character of each word in a string | Java implementation of the approach ; 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
class GFG { static void printLastChar ( String str ) { str = str + " ▁ " ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) System . out . print ( str . charAt ( i - 1 ) + " ▁ " ) ; } } public static void main ( String s [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; printLastChar ( str ) ; } }