读书人

关于南开100题的请问

发布时间: 2012-02-21 16:26:23 作者: rapoo

关于南开100题的请教
编写一个函数findStr(char *str,char *substr),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如,假定输入的字符串为 "asd asasdfg asd as zx67 asd mklo ",子字符串为 "as ",函数返回值是6。

函数ReadWrite()实现从文件in.dat中读取两个字符串,并调用函数findStr(),最后把结果输出到文件out.dat中。

注意:部分源程序存在文件prog1.c中。

请勿改动主函数main()和其它函数中的任何内容,仅在函数findStr()的花括号中填入你编写的若干语句。

#include <stdio.h>

#include <string.h>

#include <conio.h>



int findStr(char *str,char *substr)

{ int i,j,len1,len2,cnt=0,flag;

len1=strlen(str);

len2=strlen(substr);

for(i=0;i <len1;i++)

{ for(j=0;j <len2;j++)

if(str[i+j]==substr[j]) flag=1;这一行什么意思?

else {flag=0;break;}

if(flag==1) cnt++;

}

return cnt;

}





[解决办法]
int findStr(char *str,char *substr)

{ int i,j,len1,len2,cnt=0,flag;

len1=strlen(str);

len2=strlen(substr);

for(i=0;i <len1;i++)

{ for(j=0;j <len2;j++)

if(str[i+j]==substr[j]) flag=1;这一行什么意思?
//这一行是判断是否需要查找的字符串substr在str中存在,如果存在置标志,不存在则不置

else {flag=0;break;}

if(flag==1) cnt++;

}

return cnt;

}

读书人网 >C语言

热点推荐