strnicmp函数

函数名: strnicmp
功 能: 比较字符串str1和str2的前n个字符串字典序的大小,但是不区分字母大小写。
返回值: 当str1<str2时,返回值<0 ; 当str1=str2时,返回值=0; 当str1>str2时,返回值>0。
比较是这样进行的,先比较2个字符串的第1个字符字典序的大小,如果能比较出大小,则马上返回了,如果不能区别大小,开始比较第2个,如果这时第1个字符串已经到尽头了,第2个字符串还有字符,这时算第2个字符串大。

用 法: int strnicmp(char *str1, char *str2, unsigned maxlen);

#include <stdio.h>
#include <ctype.h>


/** 
 * strnicmp - Case insensitive, length-limited string comparison 
 * @s1: One string 
 * @s2: The other string 
 * @len: the maximum number of characters to compare 
 */  
int strnicmp(const char *s1, const char *s2, size_t len)  
{  
    /* Yes, Virginia, it had better be unsigned */  
    unsigned char c1, c2;  
  
    if (!len)  
        return 0;  
  
    do {  
        c1 = *s1++;  
        c2 = *s2++;  //字符串字串同时后移
        if (!c1 || !c2)  
            break;  //如果是空字符,跳出
        if (c1 == c2)  
            continue;  
        c1 = tolower(c1);  
        c2 = tolower(c2);  
        if (c1 != c2)  
            break;  
    } while (--len);  
    return (int)c1 - (int)c2;  
}  



int main(void)
{
	char *buf1 = "BBBccc", *buf2 = "bbbccc";
	int nResult;
	nResult = strnicmp(buf2, buf1, 3);
	if (nResult > 0)
	{
		printf("buffer 2 is greater than buffer 1\n");
	}
	
	if (nResult < 0)
	{
		printf("buffer 2 is less than buffer 1\n");
	}
	
	if (nResult == 0)
	{
		printf("buffer 2 equals buffer 1\n");
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值