自己实现strncasecmp

自己实现
int strncasecmp(const char *s1, const char *s2, int n)
{
#define DIFF_VALUE ('a' - 'A')
#define STRNCASECMP_FALSE ('z' - 'A')
    int ch1 = 0;
    int ch2 = 0;
    int index = 0;

    if (NULL == s1 || NULL == s2 || 0 > n) {
        return STRNCASECMP_FALSE;
    }

    if (0 == n) {
        return 0;
    }

    for (index = 0; index < n ; index++)
    {
        if((ch1 = *(unsigned char *)s1++) >= 'A' && (ch1 <= 'Z')) {
            ch1 += DIFF_VALUE;
        }
        if((ch2 = *(unsigned char *)s2++) >= 'A' && (ch2 <= 'Z')) {
            ch2 += DIFF_VALUE;
        }
        //printf("index=%u, ch1=%c, ch2=%c\r\n", index, ch1, ch2);

        if ('\0' == ch1 || ch1 != ch2) {
            break;
        }
    } ;

    return ch1 - ch2;
}

  摘自并整理:http://www.voidcn.com/article/p-acjqpctj-oc.html

posted @ 2020-01-01 01:18  LiuYanYGZ  阅读(481)  评论(0编辑  收藏  举报