`
NOthingAj
  • 浏览: 16689 次
社区版块
存档分类
最新评论

打印输入中单词长度的直方图。

    博客分类:
  • C
C 
阅读更多

打印输入中单词长度的直方图。

#include <stdio.h>

#define IN 1
#define OUT 0
#define MAX_LEN 50

main() {
    int c, len, other, i, j;
    int words[MAX_LEN+1] = {0};
    len = other = 0;

    while((c = getchar()) != EOF) {
        if(c == ' ' || c == '\t' || c == '\n') {
            if(len <= MAX_LEN) 
                ++words[len];
            else 
                ++other;
            len = 0;
        } 
        else 
            ++len;
    }
    printf("word's length  count\n");
    for(i = 1; i <= MAX_LEN; i++) {
        if(words[i] != 0) {
            printf("%13d%7d", i, words[i]);
            for(j = 0; j < words[i]; j++)
                putchar('*');
            putchar('\n');
        }
    }
    if(other != 0)
        printf("There are %d words exceed the max length 50\n");
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics