How does the C language determine strings?
In the C language, the strcmp() function can be used to determine if two strings are equal. The strcmp() function will return an integer, with a return value of 0 if the two strings are equal, a value greater than 0 if the first string is greater than the second string, and a value less than 0 if the first string is less than the second string.
Here is an example code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
int result;
printf("请输入第一个字符串:");
gets(str1);
printf("请输入第二个字符串:");
gets(str2);
result = strcmp(str1, str2);
if (result == 0) {
printf("两个字符串相等\n");
} else if (result > 0) {
printf("第一个字符串大于第二个字符串\n");
} else {
printf("第一个字符串小于第二个字符串\n");
}
return 0;
}
The gets() function is used in the above code to obtain the user input string, storing it in the specified character array.
Next, compare two strings using the strcmp() function and store the comparison result in the variable result.
Finally, output the judgment result based on the value of the result variable.