From: Cian Bagshaw Date: Wed, 25 Jan 2023 22:47:31 +0000 (+0000) Subject: 1-13 X-Git-Url: https://tests.cianb.xyz/?a=commitdiff_plain;h=e584a995ace297fccb48558f6611a63a85fde5fc;p=cProgLang 1-13 --- diff --git a/1/13.c b/1/13.c new file mode 100644 index 0000000..dc8de45 --- /dev/null +++ b/1/13.c @@ -0,0 +1,34 @@ +/* + Exercise 1-13. Write a program to print a histogram of the lengths of words in + its input. It is easy to draw the histogram with the bars horizontal; a + vertical orientation is more challenging. + === +*/ + +#include + +#define BUF 50 /* max word length */ + +int main () { + int c, l, i, lngst; /* character, length, index, longest */ + int count[BUF]; /* length count */ + + /* initialise to zero */ + for (i=0; ilngst) lngst=l; /* update longest */ + count[l-1]++; /* add length to count */ + l=0; /* reset length count */ + } + } else l++; /* count word length */ + } + while (lngst--) { /* for each count */ + printf("%2.d|", lngst+1); /* length */ + while (count[lngst]--) printf("#"); /* count (bar) */ + printf("\n"); /* newline */ + } +}