c语言如何对比字符串

   2025-02-05 7650
核心提示:C语言对比字符串需要使用strcmp函数,该函数会比较两个字符串的ASCII码值。例如,以下代码演示了如何对比两个字符串:#include s

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函数。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言