From b6b22b5c5acbe317d7ad992b58169b0678e64a9a Mon Sep 17 00:00:00 2001 From: zlg Date: Thu, 29 Nov 2012 14:44:25 -0600 Subject: 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 --- 1-13_word-length-histogram.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to '1-13_word-length-histogram.c') 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; -- cgit v1.2.3-54-g00ecf