about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authoraabacchus <ben@bvnf.space>2021-05-19 00:37:07 +0100
committeraabacchus <ben@bvnf.space>2021-05-19 00:37:07 +0100
commitea71f2d3d506df44c2a5d20c15c9f8d27428d321 (patch)
tree956ae566ec51f6522c998099a18572a223502800
parent3a14f3cf7a1bd094ae73a4d2ca151464a9eda224 (diff)
downloadkandr-ea71f2d3d506df44c2a5d20c15c9f8d27428d321.tar.gz
horizontal histogram
-rw-r--r--1.6.c39
1 files changed, 24 insertions, 15 deletions
diff --git a/1.6.c b/1.6.c
index 4ed1550..ae88521 100644
--- 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]);
+    }
 }