aboutsummaryrefslogtreecommitdiff
path: root/1-13_word-length-histogram.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2012-08-11 14:55:41 -0400
committerzlg <zlg@zlg.space>2012-08-11 14:55:41 -0400
commit569a5bd5c48d8969e644947ce3d89a97a0d56e5e (patch)
tree877a5fe80c7d36e04777ceba6d560949562c152e /1-13_word-length-histogram.c
downloadknr-569a5bd5c48d8969e644947ce3d89a97a0d56e5e.tar.gz
knr-569a5bd5c48d8969e644947ce3d89a97a0d56e5e.tar.bz2
knr-569a5bd5c48d8969e644947ce3d89a97a0d56e5e.tar.xz
knr-569a5bd5c48d8969e644947ce3d89a97a0d56e5e.zip
Initial commit
Diffstat (limited to '1-13_word-length-histogram.c')
-rw-r--r--1-13_word-length-histogram.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/1-13_word-length-histogram.c b/1-13_word-length-histogram.c
new file mode 100644
index 0000000..1b4cdc0
--- /dev/null
+++ b/1-13_word-length-histogram.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+#define IN 1
+#define OUT 0
+#define MINWLENGTH 2
+#define MAXWLENGTH 20
+
+int main(void) {
+ /* Rundown of variables:
+ c = current input char
+ state = inside or outside a word
+ ltrs = letter count
+ wrds = word count
+ lines = you should be shot if you don't know
+ lengths = an array that keeps track of how often words up to x chars long
+ occur.
+ */
+
+ int c, state, ltrs, wrds, lines, wlen;
+ int lengths[MAXWLENGTH];
+ int i;
+ for (i = 0; i <= MAXWLENGTH; ++i) {
+ lengths[i] = 0;
+ }
+
+ ltrs = wrds = wlen = 0;
+ lines = 1;
+ state = OUT;
+ // Capture input until it ends
+ while ((c = getchar()) != EOF) {
+ // If it's whitespace, we've exited a word
+ if (c == '\n' || c == ' ' || c == '\t') {
+ if (state == IN) {
+ ++wrds; // ...and should increase the count.
+ state = OUT;
+ /* Check to see if the word is eligible to be counted. */
+ if (wlen <= MAXWLENGTH) {
+ ++lengths[wlen];
+ }
+ // Reset our word length now.
+ wlen = 0;
+ }
+ /* If it's a new line, we're still out of a word but need to increment the
+ line count */
+ if (c == '\n') {
+ ++lines;
+ }
+ } else {
+ /* If nothing else, we know it's just a random character or a letter. */
+ state = IN;
+ ++wlen;
+ }
+ /* Everything that's input counts as a letter. */
+ ++ltrs;
+ }
+ // This is ugly and I wish I knew a better way to do it.
+ printf("\nWORD LENGTH FREQUENCY\n\n 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75\n");
+ int iter;
+ iter = MINWLENGTH;
+ while (iter <= MAXWLENGTH) {
+ i = lengths[iter];
+ if (i > 0) {
+ printf("%2d | ", iter);
+ while (i > 0) {
+ printf("#");
+ i = i-1;
+ }
+ printf("\n");
+ }
+ ++iter;
+ }
+ printf("%d words, %d chars, %d lines.\n", wrds, ltrs, lines);
+ return 0;
+}