Dremendo Tag Line

Input a name and print its initials in C++

String Methods - Question 4

In this question, we will see how to input a name and print its initials in C++ programming. To know more about string methods click on the string methods lesson.

Q4) Write a program in C++ to input a name and print its initials.

Example:
Input ->   MOHIT KUMAR AGARWAL
Output ->  M.K.A

Program

#include <iostream>
#include <conio.h>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    char s[50];
    int i;

    cout<<"Enter a name\n";
    gets(s);

    cout<<"The initials of the name is\n";
    for(i=0; i<strlen(s); i++)
    {
        if(i==0 && s[i]!=' ')
        {
            cout<<s[i];
        }
        else if(s[i]==' ' && s[i+1]!=' ')
        {
            cout<<"."<<s[i+1];
        }
    }
    return 0;
}

Output

Enter a name
Mohandas Karamchand Gandhi
The initials of the name is
M.K.G
video-poster