帮忙看个小程序

[复制链接]
查看11 | 回复1 | 2009-1-29 16:43:21 | 显示全部楼层 |阅读模式
编写一个函数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;
}
}
}
}
帮忙看一下这个函数,结果是错的,怎么改啊?动小手术即可。

回复

使用道具 举报

千问 | 2009-1-29 16:43:21 | 显示全部楼层
#includeint 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 c...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行