summaryrefslogtreecommitdiff
path: root/ch01
diff options
context:
space:
mode:
Diffstat (limited to 'ch01')
-rw-r--r--ch01/ex14-histogram.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/ch01/ex14-histogram.c b/ch01/ex14-histogram.c
new file mode 100644
index 0000000..6d410e1
--- /dev/null
+++ b/ch01/ex14-histogram.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+#define ASCII_FIRST_PRINTABLE_CODEPOINT 33
+#define ASCII_LAST_PRINTABLE_CODEPOINT 126
+#define ASCII_SPACE ASCII_LAST_PRINTABLE_CODEPOINT - ASCII_FIRST_PRINTABLE_CODEPOINT + 1
+
+#define BAR '#'
+
+/* print a histogram of the frequencies of different characters */
+int main()
+{
+ int c, i, j;
+ int nchar[ASCII_SPACE];
+
+ for (i = 0; i < ASCII_SPACE; ++i)
+ nchar[i] = 0;
+
+ while ((c = getchar()) != EOF)
+ if (c >= ASCII_FIRST_PRINTABLE_CODEPOINT && c <= ASCII_LAST_PRINTABLE_CODEPOINT)
+ ++nchar[c - ASCII_FIRST_PRINTABLE_CODEPOINT];
+
+ for (i = 0; i < ASCII_SPACE; ++i) {
+ if (nchar[i] > 0) {
+ printf("%c: ", i + ASCII_FIRST_PRINTABLE_CODEPOINT);
+ for (j = 0; j < nchar[i]; ++j)
+ putchar(BAR);
+ putchar('\n');
+ }
+ }
+}