早教吧 育儿知识 作业答案 考试题库 百科 知识分享

C语言统计文章中单词的个数//word.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdio.h"#include"stdlib.h"#include#include#include"string"charpassage[200][200];intmax=0;intread(){FILE*fp;inti=0;

题目详情
C语言统计文章中单词的个数// word.cpp : Defines the entry point for the console application. // #include "stdio.h" #include "stdlib.h" #include #include #include "string" char passage[200][200]; int max=0; int read() { FILE *fp; int i=0; char *p; if((fp=fopen("word.txt","r"))==NULL) return 1; while(fgets(passage[i],200,fp)!=NULL) { p=strchr(passage[i],'\n'); if(p) *p=0; i++; } max=i; fclose(fp); return 0; } void cout() { int i,j,str,flag=0; int flag1=0; for(i=0;i =0&&passage[i][j]
▼优质解答
答案和解析
#include #include #include main() { FILE *fp; char *pBuf,filename[20]; int count=0; printf("请输入要打开的文件名:\n"); scanf("%s",filename); if((fp=fopen(filename,"rb"))==NULL) {printf("无法打开文件,请注意输入后缀!\n"); exit(0); } fseek(fp,0,SEEK_END); int len=ftell(fp); pBuf=new char[len+1]; rewind(fp); fread(pBuf,1,len,fp); pBuf[len]=0; printf("%s\n",pBuf); fclose(fp); while(*pBuf!='\0') {if(*pBuf==' ') count++; pBuf++; } printf("%d",count); }