C Programming code for taking 1st Value in Strings – KLETech

Write a Modular C Programming Code for Taking 1st Value in Strings | Sentence?

CODE:

#include<stdio.h>
#include<string.h>
#define MAX_LENGTH 50
void print_short_name(char fn[], char mn[],char ln[]);
main()
{
char first_name[MAX_LENGTH];
char mid_name[MAX_LENGTH];
char last_name[MAX_LENGTH];
printf("enter your first name, middle name and last name\n");
scanf("%s%s%s",first_name, mid_name,last_name );
print_short_name( first_name, mid_name, last_name);
}
void print_short_name(char fn[], char mn[],char ln[])
{
char short_name[MAX_LENGTH];
short_name[0]=fn[0]; //Get 1st Character from first name
short_name[1]=mn[0]; //Get 1st Character from middle name
short_name[2]=ln[0]; //Get 1st Character from last name
short_name[3]='\0'; //String ends with NULL character, so add it.
printf(" short name: %s\n",short_name);
}

 

OUTPUT

enter your first name, middle name and last name
Karnataka Lingayat Education
short name: KLE

Process returned 0 (0x0) execution time : 53.152 s
Press any key to continue.

 

Please find some more codes of 1D Arrays, 2D Arrays, Pointers on the below page:

Top 100+ C Programming codes – KLE Technological University

 

Leave a Comment