字符串查找问题,找错

dmt9697 2008-10-31 10:56:02
// 从字符串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过大返回
...全文
139 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hhyttppd 2008-10-31
  • 打赏
  • 举报
回复
我这里运行正常。
cyj626 2008-10-31
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hhyttppd 的回复:]
引用 4 楼 hqin6 的回复:
能贴个全代码么??很乐意为美女效劳~~嘿嘿~~~


你是为美女头像效劳吧。。。
[/Quote]
大实话
hhyttppd 2008-10-31
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 hqin6 的回复:]
能贴个全代码么??很乐意为美女效劳~~嘿嘿~~~
[/Quote]

你是为美女头像效劳吧。。。
dmt9697 2008-10-31
  • 打赏
  • 举报
回复
谢谢了
dmt9697 2008-10-31
  • 打赏
  • 举报
回复
char* charcmp(char *pDest, int nDestLengh, char *pCmp, int nCmpLength)
{
if(pDest == NULL || nDestLengh <= 0 || nDestLengh > 1000000)
{
return NULL;
}

if(pCmp == NULL || nCmpLength <= 0 || nCmpLength > 1000000)
{
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函数中有判断,字符串pDest不会很长的
太乙 2008-10-31
  • 打赏
  • 举报
回复
能贴个全代码么??很乐意为美女效劳~~嘿嘿~~~
dmt9697 2008-10-31
  • 打赏
  • 举报
回复
像1楼这样的回复就不用回复了
dmt9697 2008-10-31
  • 打赏
  • 举报
回复
正在练习自己的逻辑能力,我就想知道我这个字符串查找问题出现在哪里,还有哪种情况没有考虑到
JIGSONG 2008-10-31
  • 打赏
  • 举报
回复

string str;
string::iterator p;
p = find(vs.begin(),vs.end(),'c');

p指向str中第一个出现c的位置。
[Quote=引用楼主 dmt9697 的帖子:]
// 从字符串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; …
[/Quote]
hhyttppd 2008-10-31
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 ForestDB 的回复:]
void* memchrlow(char *ptr, int c, int nLength)
{
if(ptr == NULL || nLength <= 0)
{
return NULL;
}
if(nLength > 1000000)
{
return NULL; // 不知道为什么会进入到这里返回
}

我就这里的问题简单的说明下吧
先来个猜测,LZ是在TC下做的功课吧
如果是的话,那么tc的int只有16位,最大值也就32767
如果还不懂我在说什么,LZ可以自行去了解下int在内存中是怎么存的,关于这方面的阐述,网络上还是很多的
[/Quote]

你的意思是1000000tc中转为int?不能吧,即使在TC中, 也应该作为long型字量(int16无法表示),然后将nLength提升到long进行比较,这然仍然不可以进入这条分支啊,没有测试过。。。
ForestDB 2008-10-31
  • 打赏
  • 举报
回复
void* memchrlow(char *ptr, int c, int nLength)
{
if(ptr == NULL || nLength <= 0)
{
return NULL;
}
if(nLength > 1000000)
{
return NULL; // 不知道为什么会进入到这里返回
}

我就这里的问题简单的说明下吧
先来个猜测,LZ是在TC下做的功课吧
如果是的话,那么tc的int只有16位,最大值也就32767
如果还不懂我在说什么,LZ可以自行去了解下int在内存中是怎么存的,关于这方面的阐述,网络上还是很多的
dmt9697 2008-10-31
  • 打赏
  • 举报
回复
charcmp函数,如果面对比较特殊的字符串的时候会出错,但是没有办法知晓这特殊字符串是什么样子的

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧