作业帮 > 综合 > 作业

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

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/06/04 06:12:41
统计某文本文件中各单词个数C语言设计
统计某文本文件中各单词个数
要求:统计出用户指定的文本文件的所有单词的个数,并把结果存入结果文件中.
统计某文本文件中各单词个数C语言设计
#include
#include
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);
}