c语言字符串的比较大小的有甚么方法
在C语言中,字符串的比较大小可以通过以下几种方法实现:
示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abc";
char str2[] = "def";
int result = strcmp(str1, str2);
if (result < 0) {
printf("str1 小于 str2
");
} else if (result > 0) {
printf("str1 大于 str2
");
} else {
printf("str1 等于 str2
");
}
return 0;
}
示例代码:
#include <stdio.h>
int compareStrings(char str1[], char str2[]) {
int i = 0;
while (str1[i] == str2[i]) {
if (str1[i] == '