C程序设计语言课后练习1.6.1

#include <stdio.h>
#define MAXWORD 15
#define IN 1
#define OUT 0
int main()
{
	int c;
	int i=0,len=0,nc=0;//NC用于记录单词的长度。
	int wordlength[MAXWORD]={0};//将输入的单词长度记录在此数组中
	int state=OUT; //用于记录是不是一个新单词
	int value=0;//用于读取wordlength[WORDMAX]中的值
	int overflow=0;//记录单词长度大于MAXWORD的次数
	int num=0;
	

	while ( ( c=getchar() ) != EOF ){
		if( c == ' ' || c == '\t' || c == '\n'){
			state = OUT;
			if( nc > 0 && nc < MAXWORD) {
				wordlength[nc]++;
				nc = 0;
			}
			else if( nc >= MAXWORD) {
				overflow++;
				nc = 0;
			}
		}
		else if ( state = OUT){
				nc++;
				state = IN;
				}
			else{
			state = IN;
			nc++;
		}
	};

	/* for( i = 1 ; i < MAXWORD ; i++){
		printf("长度为%5d的单词 - %5d个\n",i,wordlength[i]);
	}; */

	for( i = 1 ; i < MAXWORD ; i++){
		printf("%2d - %3d : ",i,wordlength[i]);
		if( wordlength[i] > 0){
			int k = 0;
			for( k = 0; k< wordlength[i] ; k++){
			putchar('*');
			}
		}
		 putchar('\n');
	};
	if( overflow > 0)
	printf("There are %d wordS overflow",overflow);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44127727/article/details/87954601