C语言比较字符串大小的代码
以下是C语言比较字符串大小的代码示例:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20], str2[20];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if (strcmp(str1, str2) == 0) {
printf("Both strings are equal.\n");
}
else if (strcmp(str1, str2) < 0) {
printf("First string is smaller than second string.\n");
}
else {
printf("First string is larger than second string.\n");
}
return 0;
}
在此示例中,我们使用了strcmp()函数来比较两个字符串的大小。如果字符串相等,则该函数返回0。如果第一个字符串小于第二个字符串,则该函数返回一个负数。如果第一个字符串大于第二个字符串,则该函数返回一个正数。我们可以使用这些值来判断两个字符串的大小关系
原文地址: https://www.cveoy.top/t/topic/eE7S 著作权归作者所有。请勿转载和采集!