从一个文本文件读取文本,显示该文本;计算文本中的单词个数,并显示统计结果。其中数字,比如“50”计为一个单词,标点符号不计入。
求解~!!
参考:https://ask.csdn.net/questions/950272 中我的回答,还有问题的话,请先采纳并重新提问。
#include<stdio.h>
#include<string.h>
void read(char *s){
int i=0;
char ch;
FILE *fp;
fp=fopen("in.txt","r");
while((ch=fgetc(fp)) != EOF){
s[i++]=ch;
}
fclose(fp);
}
int split(char *src,char *separator,char dest[][100]) {
int count=0,i=0;
char *p=strtok(src,separator);
while(p!=NULL) {
strcpy(dest[i],p);
p=strtok(NULL,separator);
count++;
i++;
}
return count;
}
int main(){
int count,i;
char src[1000],dest[100][100];
char sep[]=" \n";
read(src);
printf("%s",src);
printf("\n*****************************\n");
count=split(src,sep,dest);
for(i=0;i<count;i++){
printf("%s|",dest[i]);
}
printf("\n*****************************\n");
printf("%d",count);
return 0;
}