From ea71f2d3d506df44c2a5d20c15c9f8d27428d321 Mon Sep 17 00:00:00 2001 From: aabacchus Date: Wed, 19 May 2021 00:37:07 +0100 Subject: horizontal histogram --- 1.6.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/1.6.c b/1.6.c index 4ed1550..ae88521 100644 --- a/1.6.c +++ b/1.6.c @@ -1,20 +1,29 @@ #include +#define OUTWORD 0 +#define INWORD 1 +#define MAXWORDLEN 20 int main () { - int c, i, nwhite, nother; - int ndigit[10] = {0}; - nwhite = nother = 0; + int c, i, wordlen; + int ndigit[MAXWORDLEN] = {0}; + wordlen = 0; + char state = OUTWORD; - while ((c = getchar()) != EOF) - if (c >= '0' && c <= '9') - ++ndigit[c-'0']; - else if (c == ' ' || c == '\n' || c == '\t') - ++nwhite; - else - ++nother; - printf("digits ="); - for (i = 0; i < 10; ++i) - printf(" %d", ndigit[i]); - printf(". white space = %d, other = %d\n", - nwhite, nother); + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') { + if (state == INWORD) ++ndigit[wordlen]; + wordlen = 0; + state = OUTWORD; + } + else { + state = INWORD; + ++wordlen; + } + } + for (i = 0; i < MAXWORDLEN; ++i) { + printf("%2d | ", i); + for (int j = 0; j < ndigit[i]; ++j) + printf("="); + printf("%d\n", ndigit[i]); + } } -- cgit 1.4.1-2-gfad0