commit - 475eb200286589fa9b991140abaa9b1c917341c1
commit + ce680abd0aecc35c3967e6b940d84c652876e88e
blob - /dev/null
blob + a22dafbf6d5116dbe43af03557fe4dc05d55377c (mode 644)
--- /dev/null
+++ 1-14-text
+THE WAY TO WEALTH
+
+(From "Father Abraham's Speech," forming
+the preface to Poor _Richard's Almanac_ for 1758.)
blob - /dev/null
blob + 8885413a9614c05ef2b21143555cbbd24bbde24f (mode 644)
--- /dev/null
+++ 1-14.c
+/* 1-14 Write a program to print a histogram of the frequencies of different
+ * characters in its input. */
+
+#include <stdio.h>
+
+#define MINCHAR 32 /* only printable ASCII characters */
+#define MAXCHAR 127
+
+int main() {
+ int c; /* current character */
+ int chars[MAXCHAR+1]; /* frequencies of ASCII chars */
+ int last = 0; /* index of last ASCII char */
+ int mode = 0; /* char which occurs most frequently */
+ for (int i = MINCHAR; i <= MAXCHAR; i++) {
+ chars[i] = 0;
+ }
+ while ((c = getchar()) != EOF) {
+ chars[c]++;
+ }
+
+ /* Determine last index and mode */
+ for (int i = MINCHAR; i <= MAXCHAR; i++) {
+ if (chars[i] > 0) {
+ last = i;
+ }
+ if (chars[i] > mode) {
+ mode = chars[i];
+ }
+ }
+
+ /* Horizontal orientation */
+ for (int i = MINCHAR; i <= last; i++) {
+ if (chars[i] > 0) {
+ printf("%c: ", i);
+ for (int j = 0; j < chars[i]; j++) {
+ printf("=");
+ }
+ printf("\n");
+ }
+ }
+
+ /* Vertical orientation */
+ for (int j = mode; j > 0; j--) {
+ for (int i = MINCHAR; i <= last; i++) {
+ if (chars[i] > 0) {
+ if (chars[i] >= j) {
+ printf("|");
+ } else {
+ printf(" ");
+ }
+ }
+ }
+ printf("\n");
+ }
+ for (int i = MINCHAR; i <= last; i++) {
+ if (chars[i] > 0) {
+ printf("%c", i);
+ }
+ }
+ printf("\n");
+}