#include
#include
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;
}