函数说明 strcasecmp()用来比较参数s1和s2字符串,比较时会自动忽略大小写的差异。
返回值 若参数s1和s2字符串相同则返回0。s1长度大于s2长度则返回大于0 的值,s1 长度若小于s2 长度则返回小于0的值.
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- intstrcasecmp(constchar*s1,constchar*s2)
- {
- intc1,c2;
- do{
- c1=tolower(*s1++);
- c2=tolower(*s2++);
- }while(c1==c2&&c1!=0);
- returnc1-c2;
- }
- intmain(void)
- {
- intn=4;
- charstr1[]="Acef";
- charstr2[]="ACEFd";
- printf("strcasecmp(str1,str2)=%d/n",strcasecmp(str1,str2));
- return0;
- }
函数说明:strncasecmp()用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异
返回值 :若参数s1和s2字符串相同则返回0 s1若大于s2则返回大于0的值 s1若小于s2则返回小于0的值
- #include<stdio.h>
- #include<string.h>
- #include<ctype.h>
- intmystrncasecmp(constchar*s1,constchar*s2,intn)
- {
- intc1,c2;
- do{
- c1=tolower(*s1++);
- c2=tolower(*s2++);
- }while((--n>0)&&c1==c2&&c1!=0);
- returnc1-c2;
- }
- intmain(void)
- {
- intn=4;
- charstr3[]="ABCf";
- charstr4[]="abcd";
- printf("mystrncasecmp(str3,str4,n)=%d/n",mystrncasecmp(str3,str4,n));
- return0;
- }
本文介绍了两种用于比较字符串而不考虑大小写的C语言函数:strcasecmp() 和 strncasecmp()。这两种函数允许开发者在进行字符串匹配时忽略大小写差异,这对于实现更灵活的文本处理非常有用。
1414

被折叠的 条评论
为什么被折叠?



