diff options
author | zlg <zlg@zlg.space> | 2012-11-29 14:44:25 -0600 |
---|---|---|
committer | zlg <zlg@zlg.space> | 2012-11-29 14:44:25 -0600 |
commit | b6b22b5c5acbe317d7ad992b58169b0678e64a9a (patch) | |
tree | df8e968e05d8fd85d88c5f1db6cda37fb152aa84 /1-13_word-length-histogram.c | |
parent | Correct 1-13's chart heading (diff) | |
download | knr-b6b22b5c5acbe317d7ad992b58169b0678e64a9a.tar.gz knr-b6b22b5c5acbe317d7ad992b58169b0678e64a9a.tar.bz2 knr-b6b22b5c5acbe317d7ad992b58169b0678e64a9a.tar.xz knr-b6b22b5c5acbe317d7ad992b58169b0678e64a9a.zip |
Clean up 1-13 and 1-20, correct 1-21
1-13 uses less variables and a for loop for the chart header
1-20 has minor changes
1-21 was rewritten to behave properly
README has been rewritten to reflect the limitations of my
solutions
Diffstat (limited to '1-13_word-length-histogram.c')
-rw-r--r-- | 1-13_word-length-histogram.c | 36 |
1 files changed, 17 insertions, 19 deletions
diff --git a/1-13_word-length-histogram.c b/1-13_word-length-histogram.c index 1e21378..d3115a5 100644 --- a/1-13_word-length-histogram.c +++ b/1-13_word-length-histogram.c @@ -6,18 +6,17 @@ 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. - */ + * 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 c, state, ltrs, wrds, lines, wlen; + int state, ltrs, wrds, lines, wlen, i, j; int lengths[MAXWLENGTH]; - int i; for (i = 0; i <= MAXWLENGTH; ++i) { lengths[i] = 0; } @@ -26,9 +25,9 @@ int main(void) { lines = 1; state = OUT; // Capture input until it ends - while ((c = getchar()) != EOF) { + while ((i = getchar()) != EOF) { // If it's whitespace, we've exited a word - if (c == '\n' || c == ' ' || c == '\t') { + if (i == '\n' || i == ' ' || i == '\t') { if (state == IN) { ++wrds; // ...and should increase the count. state = OUT; @@ -41,7 +40,7 @@ int main(void) { } /* If it's a new line, we're still out of a word but need to increment the line count */ - if (c == '\n') { + if (i == '\n') { ++lines; } } else { @@ -59,19 +58,18 @@ int main(void) { } printf("\n"); // End the chart heading. - int iter; - iter = MINWLENGTH; - while (iter <= MAXWLENGTH) { - i = lengths[iter]; + j = MINWLENGTH; + while (j <= MAXWLENGTH) { + i = lengths[j]; if (i > 0) { - printf("%2d | ", iter); + printf("%2d | ", j); while (i > 0) { printf("#"); i = i-1; } printf("\n"); } - ++iter; + ++j; } printf("%d words, %d chars, %d lines.\n", wrds, ltrs, lines); return 0; |