c strcmp
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Both strings are equal.\n");
} else if (result < 0) {
printf("String 1 is lexicographically smaller than String 2.\n");
} else {
printf("String 1 is lexicographically greater than String 2.\n");
}
return 0;
}
Include necessary header files:
stdio.h
for standard input/output functions andstring.h
for string-related functions.Declare two character arrays (
str1
andstr2
) and initialize them with the strings "Hello" and "World".Use the
strcmp
function to compare the two strings (str1
andstr2
). The result is stored in the variableresult
.Check the value of
result
:- If
result
is 0, it means both strings are equal. - If
result
is negative, it meansstr1
is lexicographically smaller thanstr2
. If
result
is positive, it meansstr1
is lexicographically greater thanstr2
.Print the appropriate message based on the comparison result.
Return 0 to indicate successful execution of the program.