Capitalize the first and last character of each word in a string | CPP 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; string FirstAndLast ( string str ) { string ch = str ; for ( int i = 0 ; i < ch . length ( ) ; i ++ ) { int k = i ; while ( i < ch . length ( ) && ch [ i ] != ' ▁ ' ) i ++ ; ch [ k ] = ( char ) ( ch [ k ] >= ' a ' && ch [ k ] <= ' z ' ? ( ( int ) ch [ k ] - 32 ) : ( int ) ch [ k ] ) ; ch [ i - 1 ] = ( char ) ( ch [ i - 1 ] >= ' a ' && ch [ i - 1 ] <= ' z ' ? ( ( int ) ch [ i - 1 ] - 32 ) : ( int ) ch [ i - 1 ] ) ; } return ch ; } int main ( ) { string str = " Geeks ▁ for ▁ Geeks " ; cout << str << " STRNEWLINE " ; cout << FirstAndLast ( str ) ; }