user-0 提问: 统计单词个数，如何去掉单词后面的标点符号？

回复:
user: user-1
content: #pragma warning(disable:4018) #include <stdio.h> #include <string.h> #include <ctype.h> #include <malloc.h> #include <locale.h> int main() {     char *s,*s1,*M,*N;     int i,k,n,L,maxlen,minlen;     FILE *fp;      setlocale(LC_ALL,"chs");     fp=fopen("D:\\Exce1.txt","r");     if (fp==NULL) return 1;      s=(char *)malloc(1000*32);     if (s==NULL) return 2;      for (i=0;!feof(fp);i++) {         fscanf(fp,"%31s",s+i*32);         L=strlen(s+i*32);         if (!isalpha(s[i*32+L-1])) s[i*32+L-1]=0;         printf("第%d个单词为：%s\n",i+1,s+i*32);         if (i%1000==999) {             s1=(char *)realloc(s,(i+1+1000)*32);             if (s1==NULL) return 3;             s=s1;         }     }     printf("共%d个单词\n",i);      maxlen=strlen(s);     M=s;     for (k=32;k<i*32;k+=32) {         if (maxlen<strlen(s+k)) {             maxlen=strlen(s+k);             M=s+k;         }     }      minlen=strlen(s);     N=s;     for (n=32;n<i*32;n+=32) {         if (minlen>strlen(s+n)) {             minlen=strlen(s+n);             N=s+n;         }     }      printf("最长的单词是：%s\n, 最短的单词是：%s\n",M,N);      fclose(fp);     free(s);     return 0; }

user: user-1
content: if (i%1000==999) {//当前单词数累加到除1000的余数是999时，重新分配s的大小，增加1000个     s1=(char *)realloc(s,(i+1+1000)*32);     if (NULL==s1) return 3;     s=s1; }

user: user-0
content: 非常感谢赵4老师的回复。在你的代码基础上，我把程序增加了一个功能，显示最长和最短单词，代码如下：  #include <stdio.h> #include <string.h> #include <ctype.h> #include <malloc.h> //#include <locale.h> int main()  {     char *s,*s1;     int i,k,n,L,M,N,maxlen,minlen;     FILE *fp;       //setlocale(LC_ALL,"chs");     fp=fopen("D:\\Exce1.txt","r"); 	if (fp==NULL) return 1;  	s=(char *)malloc(1000*32); 	if (s==NULL) return 2;      for (i=0;!feof(fp);i++)  		{ 		 fscanf(fp,"%31s",s+i*32); 		 L=strlen(s+i*32);          if (!isalpha(s[i*32+L-1])) s[i*32+L-1]=0;          printf("第%d个单词为：%s\n",i+1,s+i*32);  		 //if (i%1000==999) 		 //{s1=realloc(s,(i+1+1000)*32); 		  //if(s1==NULL) return 3; 		  //s=s1; 		 //}     }     printf("共%d个单词\n",i);  	maxlen=strlen(s); 	M=0; 	for(k=1;k<(i*32);k++) 	{ 		if(maxlen<strlen(s+k)) 		{maxlen=strlen(s+k);M=int(s)+k;} 	}  	minlen=strlen(s); 	N=0; 	for(n=1;n<(i*32);n++) 	{ 		if(minlen>strlen(s+n)) 		{minlen=strlen(s+n);N=int(s)+n;} 	} 	     printf("最长的单词是：%s\n, 最短的单词是：%s\n",M,N);      fclose(fp);     free(s);     return 0; }  请问 if (i%1000==999)语句块的作用是什么？而且语句{s1=realloc(s,(i+1+1000)*32);运行时报错：cannot convert from 'void *' to 'char *' 所以我暂时注释起来了。  增加判断最长和最短单词程序块后，运行乱码，感觉是判断单词长度时出了点问题，希望得到你的更正，多谢！

user: user-1
content: 上帖代码第9行删掉。

user: user-1
content: 动态s的版本，仅供参考： #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h> #include <ctype.h> #include <locale.h> int main() {     char *s,*s1;     int N=1000;     int i;     FILE *fp;      setlocale(LC_ALL,"chs");     fp==fopen("D:\\test.txt","r");     if (NULL==fp) return 1;      s=(char *)malloc(1000*32);     if (NULL==s) return 2;     for (i=0;!feof(fp);i++) {         fscanf(fp,"%31s",s+i*32);         L=strlen(s+i*32);         if (!isalpha(s[i*32+L-1])) s[i*32+L-1]=0;         printf("第%d个单词为：%s\n",i+1,s+i*32);         if (i%1000==999) {             s1=realloc(s,(i+1+1000)*32);             if (NULL==s1) return 3;             s=s1;         }     }     printf("共%d个单词\n",i);     fclose(fp);     free(s);     return 0; }

user: user-2
content: 替换一下，把逗号都替换成空格。

user: user-1
content: #include <stdio.h> #include <string.h> #include <ctype.h> #include <locale.h> int main() {     static char s[1000][30];     int i;     FILE *fp;      setlocale(LC_ALL,"chs");     fp==fopen("D:\\test.txt","r");     if (NULL==fp) return 1;      for (i=0;i<1000 && !feof(fp);i++) {         fscanf(fp,"%29s",s[i]);         L=strlen(s[i]);         if (!isalpha(s[i][L-1])) s[i][L-1]=0;         printf("第%d个单词为：%s\n",i+1,s[i]);     }     printf("共%d个单词\n",i);     fclose(fp);     return 0; }