借鉴了一些大佬们的,终于熬出来了。
#include<stdio.h> #define OUT 0/*单词外*/ #define IN 1/*单词内*/ int main() { int c, state=OUT, len=0;/*len是单词长度*/ int nw[16];/*定义一个数组来进行储存某一长度单词的数量,然后再进行打印*/ for (int i = 1; i < 16; i++) { nw[i] = 0; } while ((c = getchar()) != EOF) { if (c != ' '& c != '\t'& c != '\n') { if (state == OUT) { state = IN; len = 1; } else if (state == IN) len++; } else { if (state == IN) { state = OUT; nw[len]++; } } } /*打印水平直方图*/ int j,k; for (j = 1; j < 16; ++j)/*输入尝试:Formulating a thesis is not the first thing you do after reading an essay assignment*/ { printf("%-5d", j); for (k = 0; k < nw[j]; k++) putchar('*'); printf("\n"); } }