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 <stdio.h>
#include <conio.h>
#include <string.h>
int main()
{
char s[50];
int i;
printf("Enter a name\n");
gets(s);
printf("The initials of the name is\n");
for(i=0; i<strlen(s); i++)
{
if(i==0 && s[i]!=' ')
{
printf("%c",s[i]);
}
else if(s[i]==' ' && s[i+1]!=' ')
{
printf(".%c",s[i+1]);
}
}
return 0;
}
Output
Enter a name Mohandas Karamchand Gandhi The initials of the name is M.K.G