当前位置: 代码迷 >> C语言 >> [求助]新人求助
  详细解决方案

[求助]新人求助

热度:111   发布时间:2006-06-16 10:18:42.0
#include "stdio.h"
#include <string>
void main()
{
int i;
int j;
int times = 0;
int cnt;
char org[100] = "asdfasdfasdfasdfasdfasdf";
char target[15] = "asd";
char *porg = org;
char *ptar = target;
for (i = 0; i < strlen(org); i++)
{
cnt = 0;
if (*(porg + i) == *ptar)
{
for (j = 0; j <= strlen(ptar); j++)
{
if (*(porg + i + j) == *(ptar + j))
{
cnt++;
}
}
if (cnt == strlen(ptar))
{
times++;
printf("find! start position: org[%d], times: %d\n", i, times);
}
}
}
}

自己测试BUG。自己改正,然后告诉我。
----------------解决方案--------------------------------------------------------
犯了巨大的错误!

结尾加上;
if (times == 0)
{
printf("notfound\n");
}
delete porg;
delete ptar;
porg = null;
ptar = null;
----------------解决方案--------------------------------------------------------
SunShining 给我改改。
----------------解决方案--------------------------------------------------------

楼主长沙人否?


----------------解决方案--------------------------------------------------------
以下是引用金为床兮银为椅在2006-6-16 10:18:42的发言:
#include "stdio.h"
#include <string> /* 笔误 */
void main()
{
int i;
int j;
int times = 0;
int cnt;
char org[100] = "asdfasdfasdfasdfasdfasdf";
char target[15] = "asd";
char *porg = org;
char *ptar = target;
for (i = 0; i < strlen(org); i++) /* i<strlen(org)-strlen(target)+1 */
{
cnt = 0;
if (*(porg + i) == *ptar)
{
for (j = 0; j <= strlen(ptar); j++) /* j< */
{
if (*(porg + i + j) == *(ptar + j))
{
cnt++;
}
}
if (cnt == strlen(ptar))
{
times++;
printf("find! start position: org[%d], times: %d\n", i, times);
}
}
}
}

自己测试BUG。自己改正,然后告诉我。


----------------解决方案--------------------------------------------------------

以下是引用金为床兮银为椅在2006-6-16 10:30:49的发言:
delete porg;
delete ptar;
porg = null;
ptar = null;

没有必要,C中main结束后数组自动释放.
----------------解决方案--------------------------------------------------------
恩 解决了就好了 这个我也刚刚做过 不久 但 现在机器不能上网 在网吧 也不能运行 就只能帮你看看

呵呵 这里高手众多 派不上我的 哈哈

看的出 楼主也是刚刚学 我们一起努力吧! 过两天我也可以天天来这里 喽
----------------解决方案--------------------------------------------------------
有没有搞错。

我用的是vc60,你说笔误不对。

我定义了指针,难道不要删除?我没有要删除数组啊?数组是不需要删除的。
----------------解决方案--------------------------------------------------------
还有你在线老隐什么身?有人追杀你呀?
----------------解决方案--------------------------------------------------------

哇 这么多人帮助新人 呵呵


----------------解决方案--------------------------------------------------------
  相关解决方案