在内存在查找子串

来源:互联网 发布:核盾网络验证 编辑:程序博客网 时间:2024/05/29 12:51
//在一段内存缓冲中查找指定字符串的位置,从头开始查找,区分大小写

//返回第一个找到的位置。
//str1 - 内存缓冲的头指针
//nLen1 - 内存缓冲长度
//str2 - 要查找匹配的字符串
bool bcompi(char byA, char byB)
{
    if (byA - byB == 0)
    {
        return true;
    }
    else
    {
        if (!((byA >= 'A' && byA <= 'Z') || (byA >= 'a' && byA <= 'z')))
            return false;

        if (!((byB >= 'A' && byB <= 'Z') || (byB >= 'a' && byB <= 'z')))
            return false;

        if ((byA - byB == 'a' - 'A') || (byB - byA == 'a' - 'A'))
            return true;
        else
            return false;
    }
}


char * memistr(const char * str1, int nLen1, const char * str2)
{
    if ((NULL == str1) || (NULL == str2) || (nLen1 <= 0))
        return NULL;
 
    long ls1 = nLen1;
    char *cp = (char *) str1;
    char *s1, *s2;
 
    if ( !*str2 )
        return((char *)str1);
 
    while (ls1 > 0)
    {
        s1 = cp;
        s2 = (char *) str2;
 
        while ( *s1 && *s2 && bcompi(*s1, *s2) )
            s1++, s2++;
 
        if (!*s2)
            return(cp);
 
        cp++;
        ls1--;
    }
 
    return(NULL);

}


0 0
原创粉丝点击