函数介绍:
strcasecmp用忽略大小写比较字符串.,通过strcasecmp函数可以指定每个字符串用于比较的字符数,strncasecmp用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异。
strcasecmp函数是二进制且对大小写不敏感。此函数只在Linux中提供,相当于windows平台的 stricmp。
函数声明:
#include<strings.h>(不是C/C++标准头文件,区别于string.h) int strcasecmp(const char *s1, const char *s2);
返回值:
若参数s1和s2字符串相等返回0,s1大于s2则返回大于0的值,s1小于s2则返回小于0的值。
实例:
/* strcasecmp.c */ #include<strings.h> #include<stdio.h> int main() { char *s1 = "aBcDeD"; char *s2 = "AbCdEd"; char *s3 = "abcdefg"; char *s4 = "bacdefg"; int len = strcasecmp(s1, s2); if (!len) { printf("%s = %s", s1, s2); } else { printf("%s != %s", s1, s2); } len = strcasecmp(s1, s3); if (!len) { printf("%s = %s", s1, s3); } else { printf("%s != %s, len = %d ", s1, s3, len); } len = strcasecmp(s1, s4); if (!len) { printf("%s = %s", s1, s4); } else { printf("%s != %s, len = %d ", s1, s4, len); } return 0; }
运行结果:
aBcDeD = AbCdEdaBcDeD != abcdefg, len = -2
aBcDeD != bacdefg, len = -1
补充:
strncasecmp()只比较前n个字符
函数定义
int strncasecmp(const char *s1, const char *s2, size_t n)