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:-

  1. Use character array to store stuff given by user.
  2. 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");
    }

Output:-

Comments

  1. Sir if i enter
    Ram Chandra Das

    ReplyDelete
    Replies
    1. Ok sir. Thank you.

      Delete
    2. It 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.

      Delete
    3. Can you do it
      Ram Chandra Das will print as R.C.Das

      Delete
    4. #include
      void 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;
      }

      Delete

Post a Comment

Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)

Popular posts from this blog

Print Fibonacci Series using Recursion | C program :