user-0 提问: c语言求txt中的单词数 最后一个单词无法计入数 求大神

回复:
user: user-1
content: 在while循环中的引用if(((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z')||(ch =='\'')) && (flag == 1))再加一个||ch == EOF应该就满足楼主的要求了吧，其他都不动。 完整代码如下： #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 200 #define M 20 void change(char &a)                        //大写字母转换成小写字母函数 {     if((a >= 'A') && (a <= 'Z'))         a+=32; } int main() {     FILE *in, *out;     int i = 0, j = 0, k = 0,x = 1, l = 0, y = 0, flag = 0, flag1 = 1, d[N];     char b[N][M], ch, z;     if((in = fopen("in.txt","r")) == NULL)	//打开输入和输出文件     {         printf("cannot open the file\n");         exit (0);     }     if((out = fopen("out.txt","w")) == NULL)     {         printf("cannot open the file\n");         exit (0);     }     printf("原文：\n");	//输出原文到屏幕和输出文件     fprintf(out,"原文：\n");     z = fgetc(in);     while (z != EOF)     {         putchar(z);         fprintf(out,"%c",z);         z = fgetc(in);     }     printf("\n");     fclose(in);     if((in = fopen("in.txt","r")) == NULL)     {         printf("cannot open the file\n");         exit (0);     }     ch = fgetc(in);	//将单词输入到二维数组中     while (ch != EOF)     {         flag = 0;         if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')||(ch =='\''))         {             change(ch);             b[i][j++] = ch;         }         else         {             flag = 1;         }         ch = fgetc(in);         if((((ch >= 'a' && ch <= 'z')||(ch >= 'A' && ch <= 'Z')||(ch =='\'')) && (flag == 1))||ch == EOF)         {             flag1 = 0;         }         if(!flag1)         {             d[l++] = j-1;             i++;             j = 0;             flag1 = 1;         }     }      printf("\n");     printf("所有单词：\n");	//输出所有单词到屏幕和输出文件     fprintf(out,"\n");     fprintf(out,"\n");     fprintf(out,"所有单词：\n");     for(j = 0; j < i; j++)     {         if(d[j] != -1)         {             y++;             for(l = 0; l <= d[j]; l++)             {                 printf("%c",b[j][l]);                 fprintf(out,"%c",b[j][l]);             }         }         printf(" ");         fprintf(out," ");     }       printf("\n");     printf("\n");     printf("词数统计：\n");	//计算和输出词数统计到屏幕和输出文件     fprintf(out,"\n");     fprintf(out,"\n");     fprintf(out,"词数统计：\n");     for(j = 0; j <= y-2; j++)     {         x = 1;         for(i = j + 1; i <= y-1; i++)         {              if((d[j] != -1) && (d[j] == d[i]))             {                 for(l = 0; l <= d[j]; l++)                 {                     if(b[j][l] != b[i][l] )                         break;                 }                 if(l == d[j]+1)                 {                     d[i] = -1;                     x++;                 }             }         }         if(i == y && d[j] != -1)         {             for(l = 0; l <= d[j]; l++)             {                 printf("%c",b[j][l]);                 fprintf(out,"%c",b[j][l]);             }             printf(" ");             fprintf(out," ");             printf("%d\n",x);             fprintf(out,"%d\n",x);         }     }     if(d[y-1] != -1)     {         for(i = 0; i <= d[y-1]; i++)         {             printf("%c",b[y-1][i]);             fprintf(out,"%c",b[y-1][i]);         }         printf(" ");         fprintf(out," ");         printf("%d\n",x);         fprintf(out,"%d\n",x);     }      fclose(in);	//关闭输入输出文件     fclose(out);     return 0; }

user: user-2
content: 仅供参考：#include <stdio.h> #include <string.h> #define MAXLEN 1000 char ln[MAXLEN]; FILE *f; int i,z; int b,n,L; int main(int argc,char **argv) {     if (argc<2) {         printf("Usage:%s fullpathfilename.ext\nget total blank/non-blank/total linenumbers.\n",argv[0]);         return 1;     }     f=fopen(argv[1],"r");     if (NULL==f) {         printf("Can not open file [%s]!\n",argv[1]);         return 2;     }     z=0;     b=0;     n=0;     L=0;     while (1) {         if (NULL==fgets(ln,MAXLEN,f)) break;         L=strlen(ln);         if ('\n'==ln[L-1]) {             if (0==z) {                 for (i=0;i<L-1;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L-1) z=1;//当前行不是空行             }             if (0==z) b++; else n++;             z=0;         } else {             if (0==z) {                 for (i=0;i<L;i++) {                     if (!(' '==ln[i] || '\t'==ln[i])) break;                 }                 if (i<L) z=1;//当前行不是空行             }         }     }     fclose(f);     if (L>0 && '\n'!=ln[L-1]) {         if (0==z) b++; else n++;//最后一行末尾无'\n'也计算     }     printf("File:[%s] total blank/non-blank/total linenumbers is %d/%d/%d\n",argv[1],b,n,b+n);     return 0; }

user: user-3
content: 刚刚看了下，你后面的单词个数统计好像是没啥问题。解雇破显示数据统计是对的。

user: user-3
content: 前面所有单词输出的部分给你找出问题了，你是每次处理完一个单词，遇到空格或换行符之类的，才把单词字符个数写入数组。可是，最后一个单词遇到EOF，所以没有执行最后一个单词的数据传输。最后加上一个就可以。然后就是你的输出循环里。单词个数是从0开始算起的，所以，j<=i就可以。后面的词数统计，是在是有些乱，看不下去，变量太多，而且解释不明确。你自己找找吧。 while (ch != EOF) 	{ 		flag = 0; 		if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '\'')) 		{ 			change(ch); 			b[i][j++] = ch; 		} 		else 		{ 			flag = 1; 		} 		ch = fgetc(in); 		if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '\'')) && (flag == 1)) 		{ 			flag1 = 0; 		} 		if (!flag1) //ADD:这里是每次前面一个单词处理完了，遇到空格或者换行才把每个单词的数据传给d[l]的；		 		{			//所以遇到最后一个EOF时，数据还没有传给数据，while循环已经退出；	 			d[l++] = j - 1; 			i++; 			j = 0; 			flag1 = 1; 		} 	} 	d[l++] = j - 1; //ADD:把你最后一个没处理完的数据加上了； 	printf("\n"); 	printf("所有单词：\n");	//输出所有单词到屏幕和输出文件 	fprintf(out, "\n"); 	fprintf(out, "\n"); 	fprintf(out, "所有单词：\n"); 	for (j = 0; j <= i; j++) // ADD:i是从0开始算起的。 	{ 		if (d[j] != -1) 		{ 			y++; 			for (l = 0; l <= d[j]; l++) 			{ 				printf("%c", b[j][l]); 				fprintf(out, "%c", b[j][l]); 			} 		} 		printf(" "); 		fprintf(out, " "); 	}