统计某文本文件中各单词个数C语言设计

来源:百度知道 编辑:UC知道 时间:2024/05/30 23:34:10
统计某文本文件中各单词个数
要求:统计出用户指定的文本文件的所有单词的个数,并把结果存入结果文件中。

#include<stdio.h>
#include<ctype.h>
void main()
{
char ch;
int numberofword=0,wordStart=0;
FILE *fp1 = fopen("test.txt","r");
FILE *fp2 = fopen("result.txt", "w");
if( fp1==NULL || fp2==NULL )
{
puts("cannot open file!");
return;
}
while( !foef(fp1) )
{
ch =fgetc(fp1);
if( isalpha(ch) && wordStart==0 )
{
wordStart = 1;
}
else if( !isalpha(ch) && wordStart==1 )
{
numberofword++;
wordStart = 0;
}
}
fprintf(fp2,"%d",numberofword);
fclose(fp1);
fclose(fp2);
}