Print the first and last character of each word in a String | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FirstAndLast ( string str ) { int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i == 0 ) cout << str [ i ] ; if ( i == str . length ( ) - 1 ) cout << str [ i ] ; if ( str [ i ] == ' ▁ ' ) { cout << str [ i - 1 ] << " ▁ " << str [ i + 1 ] ; } } } int main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; FirstAndLast ( str ) ; }