Tuesday, November 24, 2009

c program to abbreviate names


//this program will print short name
//for example : Nayan Konar will print as N.Konar

#include< stdio.h>
#include< conio.h>
void main()
{
int i,j,k;
char a[30],b[30];

clrscr();
puts("enter your name : \n");
gets(a);

printf("\n\nyour name in short is : \n\n%c",a[0]);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
printf(".%c",a[i+1]);
}
for(i=0;a[i]!='\0';i++)
{
if(a[i]==' ')
for(j=i,k=0;a[j]!='\0';j++,k++)
b[k]=a[j];
}

b[k]='\0';

for(i=2;b[i]!='\0';i++)
printf("%c",b[i]);

getch();
}

13 comments:

  1. Replies
    1. This comment has been removed by the author.

      Delete
    2. Kya thank you output to check kar lo pahle

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. This program is not fully correct because if you enter only single name like"ramesh"
    then it will show garbage value like "ringh".

    ReplyDelete
  4. Soory sir i m not fully satisfied with the program can u plz b able to explain the second for loop in which we r incrementing from which value of i and one more thing i wanna clear from u . Do value of i persist between 1st nd 2nd for loop

    ReplyDelete
  5. Soory sir i m not fully satisfied with the program can u plz b able to explain the second for loop in which we r incrementing from which value of i and one more thing i wanna clear from u . Do value of i persist between 1st nd 2nd for loop

    ReplyDelete
  6. dear shakeel, let me explain u from the begning..
    say entered name is "Mohandas Karamchand Gandhi"

    > first print statement prints the first character i.e M
    > next for loop prints the first character after every space within the name with a preceding "." so we get M.K.G
    > now the next for loop statement again check the spaces within the name and it stores the rest of the name with the space within another array i.e b
    so first it will store " Karamchand Gandhi" in b
    then it will store " Gandhi" in b

    > then we are printing b array from the 3rd element (2nd position of array) as 1st and 2nd element is processed earlier.
    so it will print "andhi"



    finally M.K.Gandhi is printed as output.



    hope it helps. :)

    ReplyDelete
    Replies
    1. I cannot understand how that loop determines the last space of tha array. I think it will check spaces, & whenever it get spaces, such as after MOHANDAS, it will start printing KARAMCHAND. But we have to determine the last space & print the characters after that space.

      Could I explain my doubt? If yes, please make me understand.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Where is the Output?

    ReplyDelete