Different types of Abbreviation of a name

#include<stdio.h>

void abbr1(char []);
void abbr2(char []);
void abbr3(char []);
int main()
{
char str[50];
printf("Enter the name : ");
gets(str);
printf("\n Abbreviated for of the name is : ");
abbr1(str);
printf("\n Abbreviated for of the name is : ");
abbr2(str);
printf("\n Abbreviated for of the name is : ");
abbr3(str);
return 0;
}

void abbr1(char str[]) // input: Rajib Kumar Das ==> output: R.K.D
{
char abbr[15];
int i,j;
abbr[0]=str[0];
for(i=1,j=1;str[i]!='\0';i++)
{
if(str[i]==' ' && str[i+1]!=' ')
{
abbr[j++]='.';
abbr[j++]=str[i+1];
}
}
abbr[j]='\0';
puts(abbr);
}

void abbr2(char str[]) // input: Rajib Kumar Das ==> output: R.K.Das
{
char abbr[15];
int i,j,t;
abbr[0]=str[0];
for(i=1,j=1;str[i]!='\0';i++)
{
if(str[i]==' ' && str[i+1]!=' ')
{
t=i+1;
abbr[j++]='.';
abbr[j++]=str[i+1];
}
}
t++;
for(;str[t]!='\0';t++)
abbr[j++]=str[t];
abbr[j]='\0';
puts(abbr);
}

void abbr3(char str[]) // input: Rajib Kumar Das ==> output: Das R.K.
{
char abbr[15];
int i,j,t;

for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ' && str[i+1]!=' ')
{
t=i+1;
}
}
for(i=t,j=0;str[i]!='\0';i++)
{
abbr[j++]=str[i];
}

abbr[j++]=' ';
abbr[j++]=str[0];
for(i=1;i<t-1;i++)
{
if(str[i]==' ' && str[i+1]!=' ')
{
abbr[j++]='.';
abbr[j++]=str[i+1];
}
}
abbr[j++]='.';
abbr[j]='\0';
puts(abbr);
}


INPUT / OUTPUT :
Enter the name : RAJIB KUMAR DAS

 Abbreviated for of the name is : R.K.D

 Abbreviated for of the name is : R.K.DAS

 Abbreviated for of the name is : DAS R.K.

Comments

Popular posts from this blog

JAVA program to add two distance

Print Pattern using C