1-14
authorCian Bagshaw <cian@cianb.xyz>
Sat, 6 May 2023 00:12:49 +0000 (01:12 +0100)
committerCian Bagshaw <cian@cianb.xyz>
Sat, 6 May 2023 00:12:49 +0000 (01:12 +0100)
1/14.c [new file with mode: 0644]

diff --git a/1/14.c b/1/14.c
new file mode 100644 (file)
index 0000000..8b6c060
--- /dev/null
+++ b/1/14.c
@@ -0,0 +1,27 @@
+/*
+       Exercise 1-14.  Write a program to print a histogram of the frequencies of
+       different characters in its input.
+       ===
+*/
+
+#include <stdio.h>
+
+#define NCHAR 256      /* number of characters */
+
+int main () {
+       int c, i, count[NCHAR];         /* character, index, character count */
+       
+       for (i=0; i<NCHAR; i++) count[i]=0;             /* initialise count to zero */
+       
+       while ((c=getchar())!=EOF)                              /* for each character */
+               if (c!=' ' && c!='\t' && c!='\n')       /* except spaces */
+                       count[c]++;                                             /* increment respective count */
+
+       for (i=0; i<NCHAR; i++)         /* for each count */
+               if (count[i]) {                 /* if there were any */
+                       printf("%c|", i);       /* print label */
+                       while (count[i]--)
+                               putchar('-');   /* bar */
+                       putchar('\n');          /* newline */
+               }
+}