diff options
Diffstat (limited to '')
-rw-r--r-- | ch1/1-03_temp-table-header.c | 3 | ||||
-rw-r--r-- | ch1/1-04_celsius_converter.c | 3 | ||||
-rw-r--r-- | ch1/1-05_backwards-table.c | 3 | ||||
-rw-r--r-- | ch1/1-08_space-counter.c | 2 | ||||
-rw-r--r-- | ch1/1-09_single-spacing.c | 2 | ||||
-rw-r--r-- | ch1/1-10_literal-escapes.c | 2 | ||||
-rw-r--r-- | ch1/1-11_word-count.c | 3 | ||||
-rw-r--r-- | ch1/1-12_one-word-per-line.c | 3 | ||||
-rw-r--r-- | ch1/1-13_word-length-histogram.c | 15 | ||||
-rw-r--r-- | ch1/1-14_character-freq-histogram.c | 8 | ||||
-rw-r--r-- | ch1/1-17_over-80.c | 12 | ||||
-rw-r--r-- | ch1/1-18_strip-blanks.c | 3 | ||||
-rw-r--r-- | ch1/1-19_reverse-lines.c | 16 | ||||
-rw-r--r-- | ch1/1-21_entab.c | 6 | ||||
-rw-r--r-- | ch1/1-22_wordwrap.c | 1 | ||||
-rw-r--r-- | ch1/1-23_decomment.c | 10 | ||||
-rw-r--r-- | ch1/1-24_syntax-checker.c | 2 |
17 files changed, 14 insertions, 80 deletions
diff --git a/ch1/1-03_temp-table-header.c b/ch1/1-03_temp-table-header.c index d0d069c..682712f 100644 --- a/ch1/1-03_temp-table-header.c +++ b/ch1/1-03_temp-table-header.c @@ -11,13 +11,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - fahr = lower; - printf(" F | C\n"); printf("------------\n"); while (fahr <= upper) { diff --git a/ch1/1-04_celsius_converter.c b/ch1/1-04_celsius_converter.c index a397d9a..921a4df 100644 --- a/ch1/1-04_celsius_converter.c +++ b/ch1/1-04_celsius_converter.c @@ -9,13 +9,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - celsius = lower; - printf(" C | F\n"); printf("------------\n"); while (celsius <= upper) { diff --git a/ch1/1-05_backwards-table.c b/ch1/1-05_backwards-table.c index a09619a..634c2a9 100644 --- a/ch1/1-05_backwards-table.c +++ b/ch1/1-05_backwards-table.c @@ -11,13 +11,10 @@ int main(void) { float fahr, celsius; int lower, upper, step; - lower = 0; upper = 300; step = 20; - fahr = upper; - printf(" F | C\n"); printf("------------\n"); while (fahr >= 0) { diff --git a/ch1/1-08_space-counter.c b/ch1/1-08_space-counter.c index 0e00176..5faf181 100644 --- a/ch1/1-08_space-counter.c +++ b/ch1/1-08_space-counter.c @@ -11,7 +11,6 @@ int main(void) { char c; int blanks, tabs, nls = 0; - while ((c = getchar()) != EOF) { if (c == ' ') { blanks++; @@ -23,7 +22,6 @@ int main(void) { nls++; } } - printf("%d blanks, %d tabs, and %d newlines.\n", blanks, tabs, nls); return 0; } diff --git a/ch1/1-09_single-spacing.c b/ch1/1-09_single-spacing.c index ab629c5..3c2d80b 100644 --- a/ch1/1-09_single-spacing.c +++ b/ch1/1-09_single-spacing.c @@ -13,7 +13,6 @@ int main(void) { char c; int spaces = 0; - while ((c = getchar()) != EOF) { if (c == ' ') { if (spaces == 0) { @@ -25,6 +24,5 @@ int main(void) { putchar(c); } } - return 0; } diff --git a/ch1/1-10_literal-escapes.c b/ch1/1-10_literal-escapes.c index 903fff4..2131ba0 100644 --- a/ch1/1-10_literal-escapes.c +++ b/ch1/1-10_literal-escapes.c @@ -12,7 +12,6 @@ int main(void) { char c; - while ((c = getchar()) != EOF) { if (c == '\t') { printf("\\t"); @@ -28,6 +27,5 @@ int main(void) { } putchar(c); } - return 0; } diff --git a/ch1/1-11_word-count.c b/ch1/1-11_word-count.c index 1b4056c..85c118e 100644 --- a/ch1/1-11_word-count.c +++ b/ch1/1-11_word-count.c @@ -17,7 +17,6 @@ int main(void) { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; - while ((c = getchar()) != EOF) { nc++; if (c == '\n') { @@ -30,8 +29,6 @@ int main(void) { nw++; } } - printf("%d %d %d\n", nl, nw, nc); - return 0; } diff --git a/ch1/1-12_one-word-per-line.c b/ch1/1-12_one-word-per-line.c index 29a38df..6e70878 100644 --- a/ch1/1-12_one-word-per-line.c +++ b/ch1/1-12_one-word-per-line.c @@ -15,7 +15,6 @@ int main(void) { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; - while ((c = getchar()) != EOF) { nc++; if (c == '\n') { @@ -34,8 +33,6 @@ int main(void) { putchar(c); } } - printf("%d %d %d\n", nl, nw, nc); - return 0; } diff --git a/ch1/1-13_word-length-histogram.c b/ch1/1-13_word-length-histogram.c index b557417..a2ee173 100644 --- a/ch1/1-13_word-length-histogram.c +++ b/ch1/1-13_word-length-histogram.c @@ -33,19 +33,17 @@ int main(void) { * 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 + /* Capture input until it ends */ while ((i = getchar()) != EOF) { - // If it's whitespace, we've exited a word + /* If it's whitespace, we've exited a word */ if (i == '\n' || i == ' ' || i == '\t') { if (state == IN) { ++wrds; // ...and should increase the count. @@ -54,7 +52,7 @@ int main(void) { if (wlen <= MAXWLENGTH) { ++lengths[wlen]; } - // Reset our word length now. + /* 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 @@ -70,13 +68,12 @@ int main(void) { /* 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. + /* End the chart heading. */ + printf("\n"); j = MINWLENGTH; while (j <= MAXWLENGTH) { i = lengths[j]; @@ -84,7 +81,7 @@ int main(void) { printf("%2d | ", j); while (i > 0) { printf("#"); - i = i-1; + i = i - 1; } printf("\n"); } diff --git a/ch1/1-14_character-freq-histogram.c b/ch1/1-14_character-freq-histogram.c index 9ef1d22..b037967 100644 --- a/ch1/1-14_character-freq-histogram.c +++ b/ch1/1-14_character-freq-histogram.c @@ -18,25 +18,21 @@ int main(void) { chars = string containing the characters the program will count lengths = the counts for each character */ - int c; int i = 0; char chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; - /* cnum is the number of characters found in the above string */ int cnum = 0; while (chars[i] != '\0') { cnum += 1; ++i; } - /* This array need its members to be initialized to zero. */ int lengths[cnum]; for (i = 0; i <= cnum; ++i) { lengths[i] = 0; } - - // Capture input until it ends + /* Capture input until it ends */ while ((c = getchar()) != EOF) { for (i = 0; i < cnum; ++i) { if (c == chars[i]) { @@ -44,7 +40,7 @@ int main(void) { } } } - // This is ugly and I wish I knew a better way to do it. + /* This is ugly and I wish I knew a better way to do it. */ printf("\nCHARACTER FREQUENCY\n\n 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75\n"); int iter = 0; while (iter <= cnum) { diff --git a/ch1/1-17_over-80.c b/ch1/1-17_over-80.c index 194e450..cbaced6 100644 --- a/ch1/1-17_over-80.c +++ b/ch1/1-17_over-80.c @@ -12,31 +12,25 @@ #define MINLENGTH 80 int main() { - // longline is used as a boolean that tells us if it's a line worth printing + /* longline is used as a boolean that tells us if it's a line worth printing */ int longline = 0; - - // len is simply a character counter, while c is the character itself. + /* len is simply a character counter, while c is the character itself. */ int len, c; char buffer[MINLENGTH]; - while ((c = getchar()) != EOF) { buffer[len] = c; - /* When we meet the end of the line, we need to print the rest of the line, * but only if we're already in a long line. Otherwise, reset our state. */ if (c == '\n') { - if (longline == 1 && len < MINLENGTH - 1) { buffer[len + 1] = '\0'; printf("%-s", buffer); } - len = 0; longline = 0; continue; } - /* When the buffer has filled up, output its contents! */ if (len == MINLENGTH) { buffer[len + 1] = '\0'; @@ -45,12 +39,10 @@ int main() { longline = 1; continue; } - /* If neither of the above cases are caught, increment our counter and fetch more data. */ ++len; } - return 0; } diff --git a/ch1/1-18_strip-blanks.c b/ch1/1-18_strip-blanks.c index 947ce8d..2074129 100644 --- a/ch1/1-18_strip-blanks.c +++ b/ch1/1-18_strip-blanks.c @@ -14,7 +14,6 @@ int get_line(char s[], int lim) { int c, i; - for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } @@ -29,7 +28,6 @@ int get_line(char s[], int lim) { int main(void) { char buffer[MAXLINELENGTH]; int c, i, len; - /* Make sure every line is gone over */ while (len = get_line(buffer, MAXLINELENGTH)) { /* An empty for statement, simply to change the i variable. @@ -37,7 +35,6 @@ int main(void) { * string terminator. */ for (i = len - 2; (i > 0) && (buffer[i] == ' ') || (buffer[i] == '\t'); --i); - /* We've reached the end of the line's actual content. Terminate the line. */ if (i >= 1) { buffer[i + 1] = '\n'; diff --git a/ch1/1-19_reverse-lines.c b/ch1/1-19_reverse-lines.c index f92c6fa..4bb83d9 100644 --- a/ch1/1-19_reverse-lines.c +++ b/ch1/1-19_reverse-lines.c @@ -23,20 +23,16 @@ int get_line(char s[], int limit) { int c, i; - for (i = 0; i < limit && (c = getchar()) != EOF && c != '\n'; ++i) { s[i] = c; } - s[i] = '\0'; - /* If I don't include this check, I can't handle blank lines */ if (c == EOF && i == 0) { return -1; } else { return i; } - } /* Directly reverse a line's contents. */ @@ -44,27 +40,23 @@ void reverse(char input[], int size) { int tmp; int i = 0; size--; - /* If len and i are the same, then there's no reason to proceed */ while (size > i) { - // Store the first character in a temporary spot... + /* Store the first character in a temporary spot... */ tmp = input[i]; - - // ... and swap! + /* ... and swap! */ input[i] = input[size]; input[size] = tmp; - - // Bring our numbers closer together + /* Bring our numbers closer together */ ++i; --size; } } int main(void) { - // An int and a string to store each line's data in + /* An int and a string to store each line's data in */ int line_len; char buffer[MAXLINELENGTH]; - while ((line_len = get_line(buffer, MAXLINELENGTH)) != -1) { reverse(buffer, line_len); printf("%s\n", buffer); diff --git a/ch1/1-21_entab.c b/ch1/1-21_entab.c index 2833520..cba68a6 100644 --- a/ch1/1-21_entab.c +++ b/ch1/1-21_entab.c @@ -19,35 +19,29 @@ int main(void) { while ((c = getchar()) != EOF) { // First thing's first, advance by a column. column++; - if (c == ' ') { /* Add to 'spaces' immediately, we'll decide if it needs to be * output later. */ spaces++; - if (column % TABWIDTH == 0 && spaces > 0) { putchar('\t'); spaces = 0; // No spaces are left when we tab! } - } else { /* Be sure to output any leftover spaces when we come across a * non-space character. This should allow for spaces between words * that don't fall along the tabstop lines. */ - while (spaces > 0) { putchar(' '); spaces--; } - // As usual, reset things on a newline. if (c == '\n') { column = 0; spaces = 0; } - // Now we can output whatever it is. putchar(c); } diff --git a/ch1/1-22_wordwrap.c b/ch1/1-22_wordwrap.c index 8cb940e..5dbeb86 100644 --- a/ch1/1-22_wordwrap.c +++ b/ch1/1-22_wordwrap.c @@ -22,7 +22,6 @@ int main() { int c, tmp, ts; int spaces = 0; int col = 0; - printf("Just type. It'll wrap to %d characters per line.\n", LINEWIDTH); while ((c = getchar()) != EOF) { if (col >= LINEWIDTH) { diff --git a/ch1/1-23_decomment.c b/ch1/1-23_decomment.c index d9c5c90..21823ef 100644 --- a/ch1/1-23_decomment.c +++ b/ch1/1-23_decomment.c @@ -43,7 +43,6 @@ int main() { if (c == '/' && status == OUT) { // Look ahead and store the character that's returned p = getchar(); - if (p == '*') { status = IN_MULTI; } else if (p == '/') { @@ -54,36 +53,27 @@ int main() { continue; } } - // Ignore everything in a single line comment until a newline if (status == IN_SINGLE) { - if (c == '\n') { putchar(c); status = OUT; continue; } - } - // Ignore everything until you reach the end of a multi comment if (status == IN_MULTI && c == '*') { p = getchar(); - if (p == '/') { status = OUT; continue; } - } - // Output everything when we're not in a comment! if (status == OUT || status == IN_STRING) { putchar(c); } - } - /* derpsauce * * diff --git a/ch1/1-24_syntax-checker.c b/ch1/1-24_syntax-checker.c index 444c562..913f859 100644 --- a/ch1/1-24_syntax-checker.c +++ b/ch1/1-24_syntax-checker.c @@ -121,7 +121,6 @@ int main() { } } } - if (escapes > 0) { printf("Invalid escape sequence on line %d!\n", linenr); return 1; @@ -162,7 +161,6 @@ int main() { printf("Unclosed comment at end of file!\n"); return 1; } - printf("All clean.\n"); return 0; } |