从文件读入一个字符串,统计该字符串中单词的个数,单词之间用空格分开,空格数可以是多个。

来源:百度知道 编辑:UC知道 时间:2024/06/17 02:46:06

//我写的 你看看对不对,我的是统计一个文件中的单词个数

#include<stdio.h>
#include<stdlib.h>
#define IN 1
#define OUT 0
int main()
{
char szFilename[256];
FILE *fp;
printf("input the file:");
scanf("%s",szFilename);
if((fp=fopen(szFilename,"r"))==NULL)
{
printf("the file don't exist!");
exit(1);
}
int flag=OUT;
int c;
int nw=0;
while((c=fgetc(fp))!=EOF)
{
if(c==' ')
{
flag=OUT;
}
else if(OUT==flag)
{
flag=IN;
nw++;
}
}
printf("THe num is:%d\n",nw);

return 0;
}