字符串查找问题,找错
// 从字符串ptr中找到字母c出现的第一个位置
void* memchrlow(char *ptr, int c, int nLength)
{
if(ptr == NULL || nLength <= 0)
{
return NULL;
}
if(nLength > 1000000)
{
return NULL; // 不知道为什么会进入到这里返回
}
int i = 0;
if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
{
for(i = 0; i < nLength; i++)
{
if(towlower(ptr[i]) == towlower(c))
{
return ptr + i;
}
}
}
else
{
for(i = 0; i < nLength; i++)
{
if(ptr[i] == c)
{
return ptr + i;
}
}
}
return NULL;
}
// 比较字符串pDest和pSoc前nLength是否完全相同
int strncmplow(char *pDest, int nDestLengh, char *pSoc, int nLength)
{
if(pSoc == NULL || nLength <= 0)
{
return 0;
}
if(pDest == NULL || nDestLengh <= 0)
{
return -1;
}
if(nDestLengh < nLength)
{
return -1;
}
for(int i = 0; i < nLength; i++)
{
if(towlower( *(pDest + i) ) != towlower( *(pSoc + i) ) )
{
return -1;
}
}
return 0;
}
// 从字符串pDest中查找字符串pCmp
char* charcmp(char *pDest, int nDestLengh, char *pCmp, int nCmpLength)
{
if(pDest == NULL || nDestLengh <= 0)
{
return NULL;
}
if(pCmp == NULL || nCmpLength <= 0)
{
return pDest;
}
if(nDestLengh < nCmpLength)
{
return NULL;
}
char *loc = (char*)memchrlow(pDest, pCmp[0], nDestLengh);
while(loc != NULL && loc - pDest < nDestLengh)
{
if(strncmplow(loc, nDestLengh - (loc - pDest), pCmp, nCmpLength) == 0)
{
return loc;
}
loc++;
loc = (char*)memchrlow(loc, pCmp[0], nDestLengh - (loc - pDest));
}
return NULL;
}
我在用charcmp查找字符串时,其中nDestLengh,nCmpLength不可能输入很大的值,可是memchrlow中会因为nLength过大返回