c语言如何对比字符串

2182
2024/2/15 14:17:47
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

C语言对比字符串需要使用strcmp函数,该函数会比较两个字符串的ASCII码值。 例如,以下代码演示了如何对比两个字符串:

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";
  
    int result = strcmp(str1, str2);
    
    if(result == 0) {
        printf("两个字符串相等\n");
    }
    else if(result < 0) {
        printf("str1小于str2\n");
    }
    else {
        printf("str1大于str2\n");
    }
    
    return 0;
}

输出结果为:

str1小于str2

如果想要忽略大小写的比较,可以使用stricmp函数代替strcmp函数。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: c语言排列组合算法怎么实现