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

#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才能使用,当单片机内存比较紧张的时候建议不要使用自带的库文件。另外一种写法见评论。

原文地址:https://www.cnblogs.com/zengxy/p/8818097.html