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