c语言,编写函数,统计输入文本中单词的个数,单词之间用空格,换行或TAB键隔开
2个回答
展开全部
设该文件名为1234.txt且在当前目录下。
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
int main(void){
FILE *fp;
char tmp[21];
int sum=0;
if((fp=fopen("1234.txt","r"))==NULL){
printf("Open the file failure...\n");
exit(0);
}
while(fscanf(fp,"%c",tmp),!(*tmp>='a' && *tmp<='z' || *tmp>='A' && *tmp<='Z'));
ungetc(*tmp,fp);
while(fscanf(fp,"%[a-zA-Z]%*[^a-z^A-Z]",tmp)>0)
sum++;
fclose(fp);
printf("There is(are) %d words in the article.\n",sum);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询