不区分大小写字符串比较函数


#include "ctype.h"

int strnicmp(char *s1, char __code *s2,  int len)
{
    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;
}

以上需要使用C语言自带的库文件ctype.h才能使用,当单片机内存比较紧张的时候建议不要使用自带的库文件。另外一种写法见评论。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM