Friday, October 30, 2009

Chap9[F]j Abbreviating names

Write a program that takes a set of names of individuals and abbreviates the first, middle and other names except the last name by their first letter.

void main()
{
char names[5][30]={
"My Name Very Very Long",
"Alien Ray Gun",
"Berry Berry White",
"Berry Berry Orange",
"Name Short"
};



int i,j,c,k,f,len,a;

for(i=0;i<5;i++)
{
for(j=0;names[i][j]!=0;j++)
{
len=strlen(&names[i][0]);
for(k=len;k>0;k--)
{
if(names[i][k]==32)
{
a=k;
break;
}
}

if(j==0)
{
c=j;

for(k=0,f=0;names[i][k]!=0;k++)
{
if(names[i][k]==32||f==1)
{
names[i][j+1]=names[i][k];
f=1;
j++;
}
}

names[i][j+1]='\0';
j=c;
}

else if(j==a)
break;

else if(names[i][j]==32&&names[i][j+2]!=32)
{
c=j;
for(k=j+2,j=j+2,f=0;names[i][k]!=0;k++)
{
if(names[i][k]==32||f==1)
{
names[i][j]=names[i][k];
f=1;
j++;
}
}

names[i][j]='\0';
j=c;
}
}
}

for(i=0;i<5;i++)
printf("\n%s",&names[i][0]);





}

2 comments: