诚心求解,关于重复调用一个嵌套函数的问题。
帮朋友做的一道题目,计算一行英文作文里某个特定的单词出现的次数,【ps那篇英文作文我以附件的形式上传上来了】要求:需要搜索的几个单词在一次输入后的到其出现次数,作文也只是一行的英文。这个程序写完没语法错误,但却无法运行,我c学得渣渣的,搞了很久都不知道到底应该怎么改进。但我肯定应该是程序最后几行中的“for(m=0;m<=2;m++){ getsentence(str1,fp,str2[m]);
printf("The word %s which you want to search in the file have been appeared %d times in total!\n",str2[m],sum);}”
出现了 错误,但又不知道应该怎么改,跪求好心人搭救orz。
a.zip
(630 Bytes)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define N 200
#define M 20
static int sum=0; /*sum是用来记录目标单词总共出现的次数,必须用static型*/
/************************************************************/
/*sum是用来记录目标单词总共出现的次数,必须用static型*/
void checkword(char* p1,char* objectword){
char temp[M];/*这个数组是用来存取从句子中提取出来的单词*/
int i=0,j,count=0;
while(p1!='\0'){
j=0;
while(isalpha(p1)){/*用来检测p1是不是字母*/
temp[j]=p1;
i++;
j++;
}
temp[j]='\0';
if(strcmp(temp,objectword)==0)/*判断提取出来的单词是否与要查找的单词一致*/
count++;
while(!isalpha(p1)&&p1!='\0'){
i++;
}
}
if(count!=0){
printf("%s\n",p1);
sum+=count;
}
}
void getsentence(char* p1,FILE* p2,char* objectword){
int i;
char ch;
ch=fgetc(p2);
while(ch!=EOF){
i=0;
while(ch!='.'&&ch!=';'&&ch!=EOF){
p1=ch;
i++;
ch=fgetc(p2);
}
if(ch=='.'||ch==';'){
p1=ch;
p1[i+1]='\0';
checkword(p1,objectword);
ch=fgetc(p2);
}
}
}
/*******************************************************************/
int main()
{
char str1[M];
char str2[2][M];
char filename[M];
int m;
FILE* fp;
printf("Please input the name of the file:\n");
scanf("%s",filename);
printf("\n");
printf("Please input the word which you want to search:\n");
scanf("%s %s %s",str2[0],str2[1],str2[2]);
fp=fopen(filename,"r");
if(fp==NULL){
printf("cannot open the file\n");
exit(0);
}
for(m=0;m<=2;m++)
{ getsentence(str1,fp,str2[m]);
printf("The word %s which you want to search in the file have been appeared %d times in total!\n",str2[m],sum);}
fclose(fp);
system("pause");
return 0;
}