c语言,编写函数,统计输入文本中单词的个数,单词之间用空格,换行或TAB键隔开

如题所述

设该文件名为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;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-31
Q+ 429884848

相似回答