Program to print the initials of a name with the surname | C ++ program to print the initials of a name with the surname ; to remove any leading or trailing spaces ; to store extracted words ; forming the word ; when space is encountered it means the name is completed and thereby extracted ; printing the first letter of the name in capital letters ; for the surname , we have to print the entire surname and not just the initial string " t " has the surname now ; first letter of surname in capital letter ; rest of the letters in small ; printing surname ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printInitials ( string str ) { int len = str . length ( ) ; str . erase ( 0 , str . find_first_not_of ( ' ▁ ' ) ) ; str . erase ( str . find_last_not_of ( ' ▁ ' ) + 1 ) ; string t = " " ; for ( int i = 0 ; i < len ; i ++ ) { char ch = str [ i ] ; if ( ch != ' ▁ ' ) t = t + ch ; else { cout << ( char ) toupper ( t [ 0 ] ) << " . ▁ " ; t = " " ; } } string temp = " " ; for ( int j = 0 ; j < t . length ( ) ; j ++ ) { if ( j == 0 ) temp = temp + ( char ) toupper ( t [ 0 ] ) ; else temp = temp + ( char ) tolower ( t [ j ] ) ; } cout << temp << endl ; } int main ( ) { string str = " ishita ▁ bhuiya " ; printInitials ( str ) ; }