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