帮忙看个小程序

来源:百度知道 编辑:UC知道 时间:2024/07/06 11:40:14
编写一个函数findstr(),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如:假定输入的字符串为"asd asasdfg asd as zx67 asd mklo",子字符串为"as",函数返回值为6。
函数readwriteDat()的功能是实现从文件in.dat中读取两个字符串,并调用函数findstr(),最后把结果输出到文件out.dat中。int findStr(char *str,char *substr)
{int cnt;
while(*str!=0)
{if(*substr!=*str)
{str++;
continue;
}
else if(*substr==*str)
{str++;
substr++;
if(*substr==*str)
{cnt++;
str++;
substr--;
}
else
{substr--;
continue;
}
}
}
}
帮忙看一下这个函数,结果是错的,怎么改啊?动小手术即可。

#include <stdio.h>

int findStr(char *str,char *substr)
{
int cnt = 0;

while( *str != '\0' )
{
if( *substr != *str )
{
str++;
continue;
}
else if(*substr == *str)
{
str++;
substr++;
if(*substr == *str)
{
cnt++;
str++;
substr--;
}
else
{
substr--;
continue;
}
}
}
return cnt;
}

int main()
{
printf("%d\n",findStr("abccabcc\0","cc\0"));

return 0;
}

如果满意的话 加点分分 新年快乐