C标准库 strnicmp函数

strnicmp()函数用于比较给定字符串指定长度的字符,不区分大小写。

strnicmp()函数 语法

int strnicmp(const char*s1,const char*s2,unsigned n);

参数s1和s2为要比较的字符串;

参数n为要比较字符串的长度。

strnicmp()函数的返回值情况:若字符串s1和s2相同,则返回0;若s1大于s2,则返回大于0的值;若s1小于s2,则返回小于0的值。

strnicmp()函数 示例

本示例使用strnicmp函数比较字符串s1和s2前3个字符的大小,不区分大小写,并将其比较结果输出。其具体代码如下所示:

#include<string.h>
#include<stdio.h>
int main(void)
{
     char*s1="COME on",*s2="computer";/*声明要比较的字符串*/
     int p;
     p=strncmpi(s2,s1,3);/*比较字符串*/
     if(p>0)
         printf("s2 is greater than s1 ");/*输出比较结果*/
     if(p<0)
         printf("s2 is less than s1 ");/*输出比较结果*/
     if(p==0)
         printf("s2 equals s1 ");/*输出比较结果*/
     return 0;
}

运行结果如图所示。

strnicmp()函数 示例


赞(1)
未经允许不得转载:极客笔记 » C标准库 strnicmp函数

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址