租用问题

质量为本、客户为根、勇于拼搏、务实创新

< 返回租用问题列表

c语言中怎么比较两个字符串,c语言中怎么比较两个字符串大小

发布时间:2024-01-09 06:32:12

c语言中怎样比较两个字符串

在C语言中,可使用strcmp函数来比较两个字符串。

strcmp函数的原型以下: int strcmp(const char *str1, const char *str2);

该函数会比较str1和str2两个字符串,并返回一个整数值来表示比较结果。如果返回值为0,表示两个字符串相等;如果返回值小于0,表示str1小于str2;如果返回值大于0,表示str1大于str2。

下面是一个比较字符串的例子: #include <stdio.h> #include <string.h>

int main() { char str1[] = “Hello”; char str2[] = “World”;

int result = strcmp(str1, str2);

if (result == 0) {
    printf("字符串相等
");
} else if (result < 0) {
    printf("str1小于str2
");
} else {
    printf("str1大于str2
");
}

return 0;

}

输出结果为: str1小于str2