Program to find the initials of a name.

Given a string name. we have to find the initial of the name


Input  : tech code bit
Output : T C B
We take the first letter of all
words and print in capital letter.

Input  : Tech Code 
Output : T C

Input  : tech code bit
Output : T B C

1) Print first character in capital.
2) Traverse rest of the string and print every character after space in capital letter.

// C++ program to print initials of a name
using namespace std;
void printInitials(const string &name)
    if (name.length() == 0)
    // Since touuper() returns int, we do typecasting
    cout << (char) toupper(name[0]);
    // Traverse rest of the string and print the
    // characters after spaces.
    for (int i=1; i<name.length()-1; i++)
        if (name[i]==' ')
            cout << " " << (char)toupper(name[i+1]);
// Driver code
int main()
    string name = "tech code bit";
    return 0;



We can also use strtok() function in C/C++ to achieve this.

Disclaimer: This does not belong to TechCodeBit, its an article taken from the below
source and credits.
source and credits:
We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at #techcodebit #google #microsoft #facebook #interview portal #jobplacements


Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar