aboutsummaryrefslogtreecommitdiff
path: root/1-13_word-length-histogram.c
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2013-02-13 20:48:44 -0600
committerzlg <zlg@zlg.space>2013-02-13 20:48:44 -0600
commit5018e06c580dd21c958ec1672c26a3448faf0c55 (patch)
treecebbb56dad0a6b821cad3712c7977f6f9b0086ab /1-13_word-length-histogram.c
parentFix 1-09's solution (diff)
downloadknr-5018e06c580dd21c958ec1672c26a3448faf0c55.tar.gz
knr-5018e06c580dd21c958ec1672c26a3448faf0c55.tar.bz2
knr-5018e06c580dd21c958ec1672c26a3448faf0c55.tar.xz
knr-5018e06c580dd21c958ec1672c26a3448faf0c55.zip
Add license file, reorganize project
Diffstat (limited to '1-13_word-length-histogram.c')
-rw-r--r--1-13_word-length-histogram.c76
1 files changed, 0 insertions, 76 deletions
diff --git a/1-13_word-length-histogram.c b/1-13_word-length-histogram.c
deleted file mode 100644
index d3115a5..0000000
--- a/1-13_word-length-histogram.c
+++ /dev/null
@@ -1,76 +0,0 @@
-#include <stdio.h>
-#define IN 1
-#define OUT 0
-#define MINWLENGTH 2
-#define MAXWLENGTH 20
-
-int main(void) {
- /* Rundown of variables:
- * i, j = reusable placeholder variables
- * 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 state, ltrs, wrds, lines, wlen, i, j;
- int lengths[MAXWLENGTH];
- for (i = 0; i <= MAXWLENGTH; ++i) {
- lengths[i] = 0;
- }
-
- ltrs = wrds = wlen = 0;
- lines = 1;
- state = OUT;
- // Capture input until it ends
- while ((i = getchar()) != EOF) {
- // If it's whitespace, we've exited a word
- if (i == '\n' || i == ' ' || i == '\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 (i == '\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;
- }
-
- printf("\nWORD LENGTH FREQUENCY\n ");
- for (i = 5; i < 80; i += 5) {
- printf(" %2d", i);
- }
-
- printf("\n"); // End the chart heading.
- j = MINWLENGTH;
- while (j <= MAXWLENGTH) {
- i = lengths[j];
- if (i > 0) {
- printf("%2d | ", j);
- while (i > 0) {
- printf("#");
- i = i-1;
- }
- printf("\n");
- }
- ++j;
- }
- printf("%d words, %d chars, %d lines.\n", wrds, ltrs, lines);
- return 0;
-}