Q3 having a string of name conta pointers to construct three new


/*Program to gets first, second and last name from name string*/
#include <stdio.h>
//main function of program
int main()
{
//variables for name. first, second and last name
char name[100] = "Mary Elizabeth Smith";
char fname[50] = "\0";
char sname[50]= "\0";
char lname[50]= "\0";
//pointer variable
char * ptr = name;
int count =0;
int i,j,k,l;
j=k=l=0;
//loop through each character in name and collect first,second and last name
for(i=0; i<strlen(ptr); i++){
if(ptr[i] == ' ')
{
count++;
}
if(count ==0)
{
fname[j++] = ptr[i];
}
if(count ==1)
{
sname[k++] = ptr[i];
}
if(count ==2)
{
lname[l++] = ptr[i];
}
}
//display first, second and last name
printf("\n fname : %s", fname);
printf("\n sname : %s", sname);
printf("\n lname : %s", lname);
return 0;
}
Step by step
Solved in 2 steps









