常用的C语言库函数实现

函数说明 strcasecmp()用来比较参数s1和s2字符串,比较时会自动忽略大小写的差异。
返回值    若参数s1和s2字符串相同则返回0。s1长度大于s2长度则返回大于0 的值,s1 长度若小于s2 长度则返回小于0的值。

int strcasecmp(const char *s1, const char *s2) 

    int c1, c2; 
    do { 
        c1 = tolower(*s1++); 
        c2 = tolower(*s2++); 
    } while(c1 == c2 && c1 != 0); 
    return c1 - c2; 
}

函数说明:strncasecmp()用来比较参数s1和s2字符串前n个字符,比较时会自动忽略大小写的差异
返回值  :若参数s1和s2字符串相同则返回0 s1若大于s2则返回大于0的值 s1若小于s2则返回小于0的值
int strnicmp(const char *s1, const char *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; 
}

字符串比较实现

int strcmp(const char* strDest,const char* strSrc)
{
    assert((strDest != NULL) && (strSrc != NULL));
    while (*strDest == *strSrc)
    {
        if (*strDest == '\0')
        {
            return 0;
        }
        ++strDest;
        ++strSrc;
    }
    return *strDest - *strSrc;
}

猜你喜欢

转载自www.linuxidc.com/Linux/2017-06/144638.htm