练习1-13:编写一个程序,打印输入中单词长度的直方图(水平)(C程序设计语言 第2版)

时间:2023-03-08 16:02:48

简单未考虑标点符号

#include <stdio.h>
#define MAX_WORD_LEN 10
#define BLANK 0
#define IN_WORD 1
#define START 2
main()
{
int c, word_len, last_ch, i, j, len_array[MAX_WORD_LEN+]; for(i=; i<MAX_WORD_LEN+; i++){
len_array[i] = ;
} i = ;
last_ch = START; while((c=getchar()) != EOF){
if (c == ' ' || c == '\t' || c == '\n'){
if (last_ch == IN_WORD){
if (i > MAX_WORD_LEN){
len_array[MAX_WORD_LEN]++;
}else{
len_array[i-]++;
}
i = ;
}
last_ch == BLANK;
}else{
last_ch = IN_WORD;
i++;
}
}
for (i=; i<MAX_WORD_LEN+; i++){
if (i == MAX_WORD_LEN){
printf(">10 ");
}else{
printf("%3d ", i+);
}
j = len_array[i];
while(j--){
printf("-");
}
printf("\n");
}
}