kandr

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit ea71f2d3d506df44c2a5d20c15c9f8d27428d321
parent 3a14f3cf7a1bd094ae73a4d2ca151464a9eda224
Author: aabacchus <ben@bvnf.space>
Date:   Wed, 19 May 2021 00:37:07 +0100

horizontal histogram

Diffstat:
M1.6.c | 39++++++++++++++++++++++++---------------
1 file changed, 24 insertions(+), 15 deletions(-)

diff --git a/1.6.c b/1.6.c @@ -1,20 +1,29 @@ #include <stdio.h> +#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]); + } }