C语言:统计一篇英文文章中所要查询单词出现的次数以及出现的位置

【问题描述】统计一篇英文文章中所要查询单词出现的次数以及出现的位置。
【基本要求】
(1) 要进行统计的英文文章为一个已经存在文件中的内容;
(2) 要查询单词运行时进行输入;
(3) 不区分大小写,如The与the看成一个单词;
【测试数据】举例:
Recently,there has been a discussion regarding the issue of knowledge in the newspaper.As can be seen from the picture,knowledge is symbolized a clenched fist as power,the fist sends out a message for "obedience",threatening to crack with power any hard nut.The more demanding and challenging a task is,the more powerful the fist seems to grow.Similarly,knowledge,the strongest power for human beings,feeds up the field of science and technology.The more advanced and developed a field is,the more profound knowledge seems to become.
该文章中单词the出现的次数为11,第一个 the出现的位置为:48,也既前面出现的字符个数。
求完整的程序,急,谢谢了.

第1个回答  2009-03-05
文件存在与程序同一目录下,文件名为test.txt
#include <stdio.h>
#include <string.h>
void change(char *a)
{
while(*a!='\0')
{
if(*a>='A'&&*a<='Z')
*a+=32;
a++;
}
}

int main(void)
{
FILE *fp;
char s[10],read[10],*p=read,ch;
int n,sum=0,sn=0,flag=0;
if((fp=fopen("test.txt","rt"))==NULL)
{
printf("\n打开文件失败");
getchar();
exit(1);
}
printf("请输入要查找的单词:\n");
gets(s);
change(s);
n=strlen(s);
ch=fgetc(fp);
if(flag==0) sn++;
while(feof(fp)!=1)
{

while(ch!=' '&&ch!=','&&ch!='.'&&ch!='!'&&ch!='?'&&ch!=':'&&ch!='"'&&ch!=';')
{
*p=ch;
p++;
ch=fgetc(fp);
if(flag==0) sn++;

}
*p='\0';
change(read);
if(strcmp(s,read)==0)
{
sum++;
flag=1;
}
ch=fgetc(fp);
if(flag==0) sn++;
p=read;
printf("%s\n",read);
}
if(flag==0) sn=n;
printf("该文章中单词%s出现的次数为%d,第一个%s出现的位置为:%d",s,sum,s,sn-n);
fclose(fp);
return 0;
}本回答被提问者采纳
相似回答