Comparing Strings
Completed
Read two words and print equal, or which one comes first alphabetically (A comes first), using strcmp.
Input
Two words.
Output
One line.
Example
Input
apple banana
Output
apple comes first
Editor
C
Hints
strcmp returns 0 if equal, negative if the first is smaller.
Solution explanation
a == b compares addresses, not text - always use strcmp.
Show solution
#include <stdio.h>
#include <string.h>
int main() {
char a[50], b[50];
scanf("%s %s", a, b);
int r = strcmp(a, b);
if (r == 0) printf("equal\n");
else if (r < 0) printf("%s comes first\n", a);
else printf("%s comes first\n", b);
return 0;
}