The strcmp(string1,string2) function compares string1 to string2. It is a value
ID: 3532211 • Letter: T
Question
The strcmp(string1,string2) function compares string1 to string2. It is a value returning function that returns a negative integer if string1 < string2, 0 if string1 == string2, and a positive integer if string1 > string2. Write a program that reads two names (last name first followed by a comma followed by the first name) and then prints them in alphabetical order. The two names should be stored in separate character arrays holding a maximum of 25 characters each. Use the strcmp() function to make the comparison of the two names. Remember that 'a' < 'b', 'b' < 'c', etc. Be sure to include the proper header file to use strcmp().
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
int main()
{
int num,i,j,k;
char name[50];
printf("Enter the number for which you want to enter the names ");
scanf("%d",&num);
char lastname[num][25];
char firstname[num][25];
for(i=0;i<num;i++)
{
printf("Enter the last name followed by first name separated by a comma ");
scanf("%s",name);
j=0;
k=0;
while(name[j]!=',')
{
lastname[i][j]=name[j];
j++;
}
j++;
while(name[j]!='')
{
firstname[i][k++]=name[j];
j++;
}
}
// Now use strcmp to sort !!
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.