Print name in a pattern | name as abbreviation | C-Program
Write a c program to display the name in the following pattern. If the name is Rahul Das than it should display R.Das .
Print abbreviation name:
Tips:-
- Use character array to store stuff given by user.
- You need to use strlen function which is present in the string.h header file to find the length.
Code:-
#include<stdio.h>
#include<string.h> //for strlen() funtion.
void main()
{
char name[20];
int var=0,n=0;
printf("Enter Your name :- ");
gets(name);
var=strlen(name);
for(int i=0;i<=var;i++)
{
if(name[i]==' ')
{ n=i+1; }
}
printf("\nYour attribute is :- ");
printf("%c.",name[0]);
for(int j=n;j<var;j++)
{
printf("%c",name[j]);
}
printf("\n\n");
}
Sir if i enter
ReplyDeleteRam Chandra Das
Ok sir. Thank you.
DeleteIt will still show 'R.Das' in output because only the first letter of the first name and the last name is taken to make the attribute, middle name is ignored.
DeleteCan you do it
DeleteRam Chandra Das will print as R.C.Das
#include
Deletevoid main()
{
char fname[20], mname[20], lname[20];
printf("Enter full name (first middle last): ");
scanf("%s %s %s", fname, mname, lname);
printf("Abbreviated name: ");
printf("%c. %c. %s\n", fname[0], mname[0], lname);
return 0;
}