笔记整理--C语言——忽略大小写的字符串查找

char* stristr(char* pString, char* pFind)
{
    unsigned long pFind_len=0;
    unsigned long cmp_len=0;

    char *pt1 = NULL, *pt2 = NULL;
    char* pString_pt = pString;
    ///////////////

    pFind_len = strlen(pFind);
    if (pFind_len == 0) {
        return(NULL);
    }

    while (*pString_pt != 0) {
        // 匹配
        pt1 = pString_pt;
        pt2 = pFind;
        cmp_len = 0;

        while ((cmp_len < pFind_len) && (*pt1 != 0)) {
            // 转成小写进行比较
            if ((*pt1 >= 'A') && (*pt1 <= 'Z') && (*pt2 >= 'a') && (*pt2 <= 'z')) {
                if((*pt1+32) != (*pt2)){ break; }
            } else if((*pt1 >= 'a') && (*pt1 <= 'z') && (*pt2 >= 'A') && (*pt2 <= 'Z')) {
                if((*pt1 - 32) != (*pt2)) {
                    break;
                }
            } else {
                if (*pt1 != *pt2) {
                    break;
                }
            }

            cmp_len++;
            pt1++;
            pt2++;
        }// while(pt1,pt2)

        // 匹配结果
        if (cmp_len == pFind_len) {
            return(pString_pt);
        }

        if (*pt1 == 0) {
             return(NULL);
        }// 匹配长度不够了

        // 继续
        pString_pt++;
    }// while(pString)

    return NULL;
}
posted @ 2023-08-16 11:23  suntl  阅读(161)  评论(0编辑  收藏  举报