From 39c0cbcc70aff47e17107826aa76cb525c83b122 Mon Sep 17 00:00:00 2001 From: Adam Carpenter Date: Tue, 9 Jul 2019 16:00:48 -0400 Subject: ch2 --- ch1/char_hist.c | 30 ++++++++++++++++++ ch1/copier.c | 29 ++++++++++++++++++ ch1/counter.c | 29 ++++++++++++++++++ ch1/ctof.c | 36 ++++++++++++++++++++++ ch1/ftoc.c | 37 +++++++++++++++++++++++ ch1/hello.c | 8 +++++ ch1/line_counter.c | 16 ++++++++++ ch1/longest_line.c | 58 +++++++++++++++++++++++++++++++++++ ch1/no_trailers.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ ch1/num_digits.c | 29 ++++++++++++++++++ ch1/one_word.c | 16 ++++++++++ ch1/power.c | 28 +++++++++++++++++ ch1/reverse.c | 70 ++++++++++++++++++++++++++++++++++++++++++ ch1/too_long.c | 39 ++++++++++++++++++++++++ ch1/wc.c | 29 ++++++++++++++++++ ch1/whitespace_converter.c | 18 +++++++++++ ch1/whitespace_counter.c | 22 ++++++++++++++ ch1/whitespace_translator.c | 26 ++++++++++++++++ ch1/word_hist.c | 24 +++++++++++++++ ch2/types | Bin 0 -> 16336 bytes ch2/types.c | 25 +++++++++++++++ char_hist.c | 30 ------------------ copier.c | 29 ------------------ counter.c | 29 ------------------ ctof.c | 36 ---------------------- ftoc.c | 37 ----------------------- hello.c | 8 ----- line_counter.c | 16 ---------- longest_line.c | 58 ----------------------------------- no_trailers.c | 72 -------------------------------------------- nohup.out | 0 num_digits.c | 29 ------------------ one_word.c | 16 ---------- power.c | 28 ----------------- reverse.c | 70 ------------------------------------------ too_long.c | 39 ------------------------ wc.c | 29 ------------------ whitespace_converter.c | 18 ----------- whitespace_counter.c | 22 -------------- whitespace_translator.c | 26 ---------------- word_hist.c | 24 --------------- 41 files changed, 641 insertions(+), 616 deletions(-) create mode 100644 ch1/char_hist.c create mode 100644 ch1/copier.c create mode 100644 ch1/counter.c create mode 100644 ch1/ctof.c create mode 100644 ch1/ftoc.c create mode 100644 ch1/hello.c create mode 100644 ch1/line_counter.c create mode 100644 ch1/longest_line.c create mode 100644 ch1/no_trailers.c create mode 100644 ch1/num_digits.c create mode 100644 ch1/one_word.c create mode 100644 ch1/power.c create mode 100644 ch1/reverse.c create mode 100644 ch1/too_long.c create mode 100644 ch1/wc.c create mode 100644 ch1/whitespace_converter.c create mode 100644 ch1/whitespace_counter.c create mode 100644 ch1/whitespace_translator.c create mode 100644 ch1/word_hist.c create mode 100644 ch2/types create mode 100644 ch2/types.c delete mode 100644 char_hist.c delete mode 100644 copier.c delete mode 100644 counter.c delete mode 100644 ctof.c delete mode 100644 ftoc.c delete mode 100644 hello.c delete mode 100644 line_counter.c delete mode 100644 longest_line.c delete mode 100644 no_trailers.c create mode 100644 nohup.out delete mode 100644 num_digits.c delete mode 100644 one_word.c delete mode 100644 power.c delete mode 100644 reverse.c delete mode 100644 too_long.c delete mode 100644 wc.c delete mode 100644 whitespace_converter.c delete mode 100644 whitespace_counter.c delete mode 100644 whitespace_translator.c delete mode 100644 word_hist.c diff --git a/ch1/char_hist.c b/ch1/char_hist.c new file mode 100644 index 0000000..312df76 --- /dev/null +++ b/ch1/char_hist.c @@ -0,0 +1,30 @@ +#include + +#define CHARSET_LENGTH 128; + +/* + * Prints a histogram of the lengths of words in its input. + */ +main() { + int c, i; + int count; + int length = CHARSET_LENGTH; + int c_counts[length]; + + for (i = 0; i < length; ++i) { + c_counts[i] = 0; + } + + while ((c = getchar()) != EOF) { + ++c_counts[c]; + } + + for (i = 0; i < length; ++i) { + count = c_counts[i]; + + if (count > 0 && (i != ' ' && i != '\t' && i != '\n' )) { + printf("%c\t", i); + printf("%d\n", count); + } + } +} diff --git a/ch1/copier.c b/ch1/copier.c new file mode 100644 index 0000000..eeade8e --- /dev/null +++ b/ch1/copier.c @@ -0,0 +1,29 @@ +#include + +/* Copy input to output; 1st version */ +//main() { +// int c; +// +// c = getchar(); +// +// while (c != EOF) { +// putchar(c); +// c = getchar(); +// } +//} + +/* Copy input to output; 2nd version */ +main() { + int c; + + while ((c = getchar()) != EOF) { + putchar(c); + } +} + +//main() { +// printf("%d", EOF); +// getchar(); +// printf("%d", getchar()); +// printf("%d", getchar()); +//} diff --git a/ch1/counter.c b/ch1/counter.c new file mode 100644 index 0000000..a1b9927 --- /dev/null +++ b/ch1/counter.c @@ -0,0 +1,29 @@ +#include + +/* + * Count characters in input; 1st version + */ +//main() { +// long nc; +// +// nc = 0; +// +// while (getchar() != EOF) +// ++nc; +// +// +// printf("%ld\n", nc); +//} + +/* + * Count characters in input; 2nd version + */ +main() { + long nc; + + + for (nc = 0; getchar() != EOF; ++nc) + ; + + printf("%ld\n", nc); +} diff --git a/ch1/ctof.c b/ch1/ctof.c new file mode 100644 index 0000000..b95e20a --- /dev/null +++ b/ch1/ctof.c @@ -0,0 +1,36 @@ +#include + +#define LOWER 0; +#define UPPER 300; +#define STEP 20; + +float ctof(int c); + +/* + * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = LOWER; // lower limit of temperature table + upper = UPPER; // upper limit of temperature table + step = STEP; // step size + + celcius = lower; + + printf("%3s %6s\n", "C", "F"); + + while (celcius <= upper) { + fahr = ctof(celcius); + printf("%3.0f %6.1f\n", celcius, fahr); + celcius = celcius + step; + } +} + +/* + * Convert Celcius to Fahrenheit + */ +float ctof(int celcius) { + return (9.0 / 5.0) * celcius + 32.0; +} diff --git a/ch1/ftoc.c b/ch1/ftoc.c new file mode 100644 index 0000000..70b994a --- /dev/null +++ b/ch1/ftoc.c @@ -0,0 +1,37 @@ +#include + +#define LOWER 0 // lower limit of table +#define UPPER 300 // upper limit of table +#define STEP 20 // step size + +float ftoc(int fahr); + +/* + * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300. + */ +main() { + float fahr, celcius; + int lower, upper, step; + + lower = LOWER; // lower limit of temperature table + upper = UPPER; // upper limit of temperature table + step = STEP; // step size + + fahr = lower; + + printf("%3s %6s\n", "F", "C"); + + // while (fahr <= upper) { + // celcius = 5.0 / 9.0 * (fahr - 32.0); + // printf("%3.0f %6.1f\n", fahr, celcius); + // fahr = fahr + step; + // } + + for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) { + printf("%3.0f %6.1f\n", fahr, ftoc(fahr)); + } +} + +float ftoc(int fahr) { + return (5.0 / 9.0) * (fahr - 32); +} diff --git a/ch1/hello.c b/ch1/hello.c new file mode 100644 index 0000000..b1ad928 --- /dev/null +++ b/ch1/hello.c @@ -0,0 +1,8 @@ +#include + +main() { + printf("hello, "); + printf("world."); + printf("\n"); +} + diff --git a/ch1/line_counter.c b/ch1/line_counter.c new file mode 100644 index 0000000..0447aa7 --- /dev/null +++ b/ch1/line_counter.c @@ -0,0 +1,16 @@ +#include + +/* + * Count lines in input + */ +main() { + int c, nl; + + nl = 0; + + while ((c = getchar()) != EOF) + if (c == '\n') + ++nl; + + printf("%d\n", nl); +} diff --git a/ch1/longest_line.c b/ch1/longest_line.c new file mode 100644 index 0000000..58f2a72 --- /dev/null +++ b/ch1/longest_line.c @@ -0,0 +1,58 @@ +#include +#define MAXLENGTH 1000 + +int get_line(char line[], int maxlength); +void copy(char to[], char from[]); + +/* + * MAIN + * Prints longest line in STDIN. + */ +main() { + int len; // current line length + int max; // maximum length seen so far + char line[MAXLENGTH]; // current input line + char longest[MAXLENGTH]; // longest line saved here + + max = 0; + while ((len = get_line(line, MAXLENGTH)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) // there was a line + printf("%d\t", max); + printf("%s", longest); + return 0; +} + +/* + * GET_LINE + * Read STDIN into LINE up to MAXLENGTH and return its length. + * Returns the length of a the line. + */ +int get_line(char line[], int maxlength) { + int c, i; + + for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) + line[i] = c; + if (c == '\n') { + line[i] = c; + ++i; + } + line[i] = '\0'; + return i; +} + +/* + * COPY + * Copy FROM into TO; assume TO is big enough. + */ +void copy(char to[], char from[]) { + int i; + + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} + diff --git a/ch1/no_trailers.c b/ch1/no_trailers.c new file mode 100644 index 0000000..80dbc0f --- /dev/null +++ b/ch1/no_trailers.c @@ -0,0 +1,72 @@ +#include + +#define MAXLENGTH 1000 + +int get_line(char line[], int maxlength); +void strip_line(char line[], int length); +void copy(char to[], char from[]); + +/* + * MAIN + * Removes all trailing whitespace and blank lines from STDIN. + */ +int main() { + int len; // current line length + char line[MAXLENGTH]; // current input line + + while ((len = get_line(line, MAXLENGTH)) > 0) { + strip_line(line, len); + if (line[0] != '\0') + printf("%s\n", line); + } + return 0; +} + +/* + * GET_LINE + * Read STDIN into LINE up to MAXLENGTH and return its length. + * Returns the length of a the line. + */ +int get_line(char line[], int maxlength) { + int c, i; + + for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) + line[i] = c; + if (c == '\n') { + line[i] = c; + ++i; + } + line[i] = '\0'; + return i; +} + +/* + * STRIP_LINE + * Remove all whitespace from end of LINE of given LENGTH. + */ +void strip_line(char line[], int length) { + int i, c; + + for (i = --length; i >= 0; --i) { + c = line[i]; + + if (c == ' ' || c == '\t' || c == '\n') + line[i] = '\0'; + else + break; + } +} + + +/* + * COPY + * Copy FROM into TO; assume TO is big enough. + */ +void copy(char to[], char from[]) { + int i; + + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} + diff --git a/ch1/num_digits.c b/ch1/num_digits.c new file mode 100644 index 0000000..c25fc29 --- /dev/null +++ b/ch1/num_digits.c @@ -0,0 +1,29 @@ +#include + +/* + * Count digits, whitespace, and other characters. + */ +main() { + int c, i, nwhite, nother; + int ndigit[10]; + + nwhite = nother = 0; + + for (i = 0; i < 10; ++i) + ndigit[i] = 0; + + while ((c = getchar()) != EOF) + if (c >= '0' && c <= '9') + ++ndigit[c - '0']; + else if (c == ' ' || c == '\n' || c == '\t') + ++nwhite; + else + ++nother; + + printf("digits ="); + + for (i = 0; i < 10; ++i) + printf(" %d", ndigit[i]); + printf(", whitespace = %d, other = %d\n", + nwhite, nother); +} diff --git a/ch1/one_word.c b/ch1/one_word.c new file mode 100644 index 0000000..8d50542 --- /dev/null +++ b/ch1/one_word.c @@ -0,0 +1,16 @@ +#include + +/* + * Print input one word per line. + */ +main() { + int c; + + while ((c = getchar()) != EOF) { + if (c == ' ' || c == '\n' || c == '\t') + putchar('\n'); + else { + putchar(c); + } + } +} diff --git a/ch1/power.c b/ch1/power.c new file mode 100644 index 0000000..8bca4fd --- /dev/null +++ b/ch1/power.c @@ -0,0 +1,28 @@ +#include + +int power(int m, int n); + +/* + * Test power function + */ +main() { + int i; + + for (i = 0; i < 10; ++i) + printf("%d %d %d\n", i, power(2, i), power(-3, i)); + + return 0; +} + +/* + * Raise base to n-th power; n >= 0 + */ +int power(int base, int n) { + int i, p; + + p = 1; + + for (i = 1; i <= n; ++i) + p = p * base; + return p; +} diff --git a/ch1/reverse.c b/ch1/reverse.c new file mode 100644 index 0000000..98edd98 --- /dev/null +++ b/ch1/reverse.c @@ -0,0 +1,70 @@ +#include + +#define MAXLENGTH 1000 + +// Prototypes +int get_line(char line[], int maxlength); +void line_copy(char to[], char from[]); +void reverse_copy(char to[], char from[], int length); + +/* + * MAIN + * Reverse all lines on STDIN. + */ +int main() { + int len; // current line length + char line[MAXLENGTH]; // current input line + char reversed_line[MAXLENGTH]; // current reversed line + + while ((len = get_line(line, MAXLENGTH)) > 0) { + reverse_copy(reversed_line, line, len); + printf("%s\n", reversed_line); + } + return 0; +} + +/* + * GET_LINE + * Read STDIN into LINE up to MAXLENGTH. + * Returns the length of LINE. + */ +int get_line(char line[], int maxlength) { + int c, i; + + for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) + line[i] = c; + if (c == '\n') { + line[i] = c; + ++i; + } + line[i] = '\0'; + return i; +} + +/* + * LINE_COPY + * Copy FROM into TO; assume TO is big enough. + */ +void line_copy(char to[], char from[]) { + int i; + + i = 0; + while ((to[i] = from[i]) != '\0') + ++i; +} + +/* + * REVERSE_COPY + * Copy FROM into TO (of length LENGTH) in reverse order; assume TO is big + * enough. + */ +void reverse_copy(char to[], char from[], int length) { + int i; + + for (i = 0; i < length; ++i) { + to[length - 1 - i] = from[i]; + } + + to[length] = '\0'; +} + diff --git a/ch1/too_long.c b/ch1/too_long.c new file mode 100644 index 0000000..dcc6ed4 --- /dev/null +++ b/ch1/too_long.c @@ -0,0 +1,39 @@ +#include + +#define MAXLENGTH 1000 + +int get_line(char line[], int maxlength); +void copy(char to[], char from[]); + +/* + * MAIN + * Print all lines larger than 80 characters. + */ +main() { + int len; // current line length + char line[MAXLENGTH]; // current input line + + while ((len = get_line(line, MAXLENGTH)) > 0) + if (len > 80) { + printf("%s", line); + } + return 0; +} + +/* + * GET_LINE + * Read STDIN into LINE up to MAXLENGTH and return its length. + * Returns the length of a the line. + */ +int get_line(char line[], int maxlength) { + int c, i; + + for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) + line[i] = c; + if (c == '\n') { + line[i] = c; + ++i; + } + line[i] = '\0'; + return i; +} diff --git a/ch1/wc.c b/ch1/wc.c new file mode 100644 index 0000000..4a5007d --- /dev/null +++ b/ch1/wc.c @@ -0,0 +1,29 @@ +#include + +#define IN 1 // inside a word +#define OUT 0 // outside a word + +/* + * Count lines, words, and characters in input. + */ +main() { + int c, nl, nw, nc, state; + + state = OUT; + nl = nw = nc = 0; + + while ((c = getchar()) != EOF) { + ++nc; + + if (c == '\n') + ++nl; + if (c == ' ' || c == '\n' || c == '\t') + state = OUT; + else if (state == OUT) { + state = IN; + ++nw; + } + } + + printf("%d %d %d\n", nl, nw, nc); +} diff --git a/ch1/whitespace_converter.c b/ch1/whitespace_converter.c new file mode 100644 index 0000000..6fee3bd --- /dev/null +++ b/ch1/whitespace_converter.c @@ -0,0 +1,18 @@ +#include + +/* + * Convert concurrent blanks to single blank. + */ +main() { + int c; + + while ((c = getchar()) != EOF) { + if (c == ' ') { + while ((c = getchar()) == ' ') + ; + putchar(' '); + } + + putchar(c); + } +} diff --git a/ch1/whitespace_counter.c b/ch1/whitespace_counter.c new file mode 100644 index 0000000..ea441e2 --- /dev/null +++ b/ch1/whitespace_counter.c @@ -0,0 +1,22 @@ +#include + +/* + * Count whitespace in input + */ +main() { + int c, n_whitespace; + + n_whitespace = 0; + + while ((c = getchar()) != EOF) { + if (c == '\n') + ++n_whitespace; + if (c == '\t') + ++n_whitespace; + if (c == ' ') + ++n_whitespace; + } + + + printf("%d\n", n_whitespace); +} diff --git a/ch1/whitespace_translator.c b/ch1/whitespace_translator.c new file mode 100644 index 0000000..efb52b2 --- /dev/null +++ b/ch1/whitespace_translator.c @@ -0,0 +1,26 @@ +#include + +/* + * Translate invisible whitespace characters to visible + * representations. + */ +main() { + int c; + + while ((c = getchar()) != EOF) { + if (c == 8) { + printf("\\b"); + continue; + } + if (c == 9) { + printf("\\t"); + continue; + } + if (c == '\\') { + printf("\\\\"); + continue; + } + + putchar(c); + } +} diff --git a/ch1/word_hist.c b/ch1/word_hist.c new file mode 100644 index 0000000..965c1b9 --- /dev/null +++ b/ch1/word_hist.c @@ -0,0 +1,24 @@ +#include + +/* + * Prints a histogram of the lengths of words in its input. + */ +main() { + int c; + int count; + + while ((c = getchar()) != EOF) { + if (c == '\n' || c == '\t' || c == ' ') { + + while (count > 0) { + printf("%s", "*"); + --count; + } + + if (count > 0) { + printf("\n"); + } + } else + ++count; + } +} diff --git a/ch2/types b/ch2/types new file mode 100644 index 0000000..bc09907 Binary files /dev/null and b/ch2/types differ diff --git a/ch2/types.c b/ch2/types.c new file mode 100644 index 0000000..8ef4c91 --- /dev/null +++ b/ch2/types.c @@ -0,0 +1,25 @@ +#include + +int stringlen(char s[]) { + int i; + + i = 0; + while (s[i] != '\0') + ++i; + return i; +} + +int main() { + //int count = stringlen("test"); + //printf("%d\n", count); + + + //enum fruit { + // APPLE, + // PEAR, + // BANANA, + //}; + //printf("%d\n", PEAR); + + +} diff --git a/char_hist.c b/char_hist.c deleted file mode 100644 index 312df76..0000000 --- a/char_hist.c +++ /dev/null @@ -1,30 +0,0 @@ -#include - -#define CHARSET_LENGTH 128; - -/* - * Prints a histogram of the lengths of words in its input. - */ -main() { - int c, i; - int count; - int length = CHARSET_LENGTH; - int c_counts[length]; - - for (i = 0; i < length; ++i) { - c_counts[i] = 0; - } - - while ((c = getchar()) != EOF) { - ++c_counts[c]; - } - - for (i = 0; i < length; ++i) { - count = c_counts[i]; - - if (count > 0 && (i != ' ' && i != '\t' && i != '\n' )) { - printf("%c\t", i); - printf("%d\n", count); - } - } -} diff --git a/copier.c b/copier.c deleted file mode 100644 index eeade8e..0000000 --- a/copier.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -/* Copy input to output; 1st version */ -//main() { -// int c; -// -// c = getchar(); -// -// while (c != EOF) { -// putchar(c); -// c = getchar(); -// } -//} - -/* Copy input to output; 2nd version */ -main() { - int c; - - while ((c = getchar()) != EOF) { - putchar(c); - } -} - -//main() { -// printf("%d", EOF); -// getchar(); -// printf("%d", getchar()); -// printf("%d", getchar()); -//} diff --git a/counter.c b/counter.c deleted file mode 100644 index a1b9927..0000000 --- a/counter.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -/* - * Count characters in input; 1st version - */ -//main() { -// long nc; -// -// nc = 0; -// -// while (getchar() != EOF) -// ++nc; -// -// -// printf("%ld\n", nc); -//} - -/* - * Count characters in input; 2nd version - */ -main() { - long nc; - - - for (nc = 0; getchar() != EOF; ++nc) - ; - - printf("%ld\n", nc); -} diff --git a/ctof.c b/ctof.c deleted file mode 100644 index b95e20a..0000000 --- a/ctof.c +++ /dev/null @@ -1,36 +0,0 @@ -#include - -#define LOWER 0; -#define UPPER 300; -#define STEP 20; - -float ctof(int c); - -/* - * Print Celcius-Fahrenheit table for celcius = 0, 20, ... 300. - */ -main() { - float fahr, celcius; - int lower, upper, step; - - lower = LOWER; // lower limit of temperature table - upper = UPPER; // upper limit of temperature table - step = STEP; // step size - - celcius = lower; - - printf("%3s %6s\n", "C", "F"); - - while (celcius <= upper) { - fahr = ctof(celcius); - printf("%3.0f %6.1f\n", celcius, fahr); - celcius = celcius + step; - } -} - -/* - * Convert Celcius to Fahrenheit - */ -float ctof(int celcius) { - return (9.0 / 5.0) * celcius + 32.0; -} diff --git a/ftoc.c b/ftoc.c deleted file mode 100644 index 70b994a..0000000 --- a/ftoc.c +++ /dev/null @@ -1,37 +0,0 @@ -#include - -#define LOWER 0 // lower limit of table -#define UPPER 300 // upper limit of table -#define STEP 20 // step size - -float ftoc(int fahr); - -/* - * Print Fahrenheit-Celcius table for fahr = 0, 20, ... 300. - */ -main() { - float fahr, celcius; - int lower, upper, step; - - lower = LOWER; // lower limit of temperature table - upper = UPPER; // upper limit of temperature table - step = STEP; // step size - - fahr = lower; - - printf("%3s %6s\n", "F", "C"); - - // while (fahr <= upper) { - // celcius = 5.0 / 9.0 * (fahr - 32.0); - // printf("%3.0f %6.1f\n", fahr, celcius); - // fahr = fahr + step; - // } - - for (fahr = UPPER; fahr >= LOWER; fahr = fahr - STEP) { - printf("%3.0f %6.1f\n", fahr, ftoc(fahr)); - } -} - -float ftoc(int fahr) { - return (5.0 / 9.0) * (fahr - 32); -} diff --git a/hello.c b/hello.c deleted file mode 100644 index b1ad928..0000000 --- a/hello.c +++ /dev/null @@ -1,8 +0,0 @@ -#include - -main() { - printf("hello, "); - printf("world."); - printf("\n"); -} - diff --git a/line_counter.c b/line_counter.c deleted file mode 100644 index 0447aa7..0000000 --- a/line_counter.c +++ /dev/null @@ -1,16 +0,0 @@ -#include - -/* - * Count lines in input - */ -main() { - int c, nl; - - nl = 0; - - while ((c = getchar()) != EOF) - if (c == '\n') - ++nl; - - printf("%d\n", nl); -} diff --git a/longest_line.c b/longest_line.c deleted file mode 100644 index 58f2a72..0000000 --- a/longest_line.c +++ /dev/null @@ -1,58 +0,0 @@ -#include -#define MAXLENGTH 1000 - -int get_line(char line[], int maxlength); -void copy(char to[], char from[]); - -/* - * MAIN - * Prints longest line in STDIN. - */ -main() { - int len; // current line length - int max; // maximum length seen so far - char line[MAXLENGTH]; // current input line - char longest[MAXLENGTH]; // longest line saved here - - max = 0; - while ((len = get_line(line, MAXLENGTH)) > 0) - if (len > max) { - max = len; - copy(longest, line); - } - if (max > 0) // there was a line - printf("%d\t", max); - printf("%s", longest); - return 0; -} - -/* - * GET_LINE - * Read STDIN into LINE up to MAXLENGTH and return its length. - * Returns the length of a the line. - */ -int get_line(char line[], int maxlength) { - int c, i; - - for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) - line[i] = c; - if (c == '\n') { - line[i] = c; - ++i; - } - line[i] = '\0'; - return i; -} - -/* - * COPY - * Copy FROM into TO; assume TO is big enough. - */ -void copy(char to[], char from[]) { - int i; - - i = 0; - while ((to[i] = from[i]) != '\0') - ++i; -} - diff --git a/no_trailers.c b/no_trailers.c deleted file mode 100644 index 80dbc0f..0000000 --- a/no_trailers.c +++ /dev/null @@ -1,72 +0,0 @@ -#include - -#define MAXLENGTH 1000 - -int get_line(char line[], int maxlength); -void strip_line(char line[], int length); -void copy(char to[], char from[]); - -/* - * MAIN - * Removes all trailing whitespace and blank lines from STDIN. - */ -int main() { - int len; // current line length - char line[MAXLENGTH]; // current input line - - while ((len = get_line(line, MAXLENGTH)) > 0) { - strip_line(line, len); - if (line[0] != '\0') - printf("%s\n", line); - } - return 0; -} - -/* - * GET_LINE - * Read STDIN into LINE up to MAXLENGTH and return its length. - * Returns the length of a the line. - */ -int get_line(char line[], int maxlength) { - int c, i; - - for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) - line[i] = c; - if (c == '\n') { - line[i] = c; - ++i; - } - line[i] = '\0'; - return i; -} - -/* - * STRIP_LINE - * Remove all whitespace from end of LINE of given LENGTH. - */ -void strip_line(char line[], int length) { - int i, c; - - for (i = --length; i >= 0; --i) { - c = line[i]; - - if (c == ' ' || c == '\t' || c == '\n') - line[i] = '\0'; - else - break; - } -} - - -/* - * COPY - * Copy FROM into TO; assume TO is big enough. - */ -void copy(char to[], char from[]) { - int i; - - i = 0; - while ((to[i] = from[i]) != '\0') - ++i; -} - diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000..e69de29 diff --git a/num_digits.c b/num_digits.c deleted file mode 100644 index c25fc29..0000000 --- a/num_digits.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -/* - * Count digits, whitespace, and other characters. - */ -main() { - int c, i, nwhite, nother; - int ndigit[10]; - - nwhite = nother = 0; - - for (i = 0; i < 10; ++i) - ndigit[i] = 0; - - while ((c = getchar()) != EOF) - if (c >= '0' && c <= '9') - ++ndigit[c - '0']; - else if (c == ' ' || c == '\n' || c == '\t') - ++nwhite; - else - ++nother; - - printf("digits ="); - - for (i = 0; i < 10; ++i) - printf(" %d", ndigit[i]); - printf(", whitespace = %d, other = %d\n", - nwhite, nother); -} diff --git a/one_word.c b/one_word.c deleted file mode 100644 index 8d50542..0000000 --- a/one_word.c +++ /dev/null @@ -1,16 +0,0 @@ -#include - -/* - * Print input one word per line. - */ -main() { - int c; - - while ((c = getchar()) != EOF) { - if (c == ' ' || c == '\n' || c == '\t') - putchar('\n'); - else { - putchar(c); - } - } -} diff --git a/power.c b/power.c deleted file mode 100644 index 8bca4fd..0000000 --- a/power.c +++ /dev/null @@ -1,28 +0,0 @@ -#include - -int power(int m, int n); - -/* - * Test power function - */ -main() { - int i; - - for (i = 0; i < 10; ++i) - printf("%d %d %d\n", i, power(2, i), power(-3, i)); - - return 0; -} - -/* - * Raise base to n-th power; n >= 0 - */ -int power(int base, int n) { - int i, p; - - p = 1; - - for (i = 1; i <= n; ++i) - p = p * base; - return p; -} diff --git a/reverse.c b/reverse.c deleted file mode 100644 index 98edd98..0000000 --- a/reverse.c +++ /dev/null @@ -1,70 +0,0 @@ -#include - -#define MAXLENGTH 1000 - -// Prototypes -int get_line(char line[], int maxlength); -void line_copy(char to[], char from[]); -void reverse_copy(char to[], char from[], int length); - -/* - * MAIN - * Reverse all lines on STDIN. - */ -int main() { - int len; // current line length - char line[MAXLENGTH]; // current input line - char reversed_line[MAXLENGTH]; // current reversed line - - while ((len = get_line(line, MAXLENGTH)) > 0) { - reverse_copy(reversed_line, line, len); - printf("%s\n", reversed_line); - } - return 0; -} - -/* - * GET_LINE - * Read STDIN into LINE up to MAXLENGTH. - * Returns the length of LINE. - */ -int get_line(char line[], int maxlength) { - int c, i; - - for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) - line[i] = c; - if (c == '\n') { - line[i] = c; - ++i; - } - line[i] = '\0'; - return i; -} - -/* - * LINE_COPY - * Copy FROM into TO; assume TO is big enough. - */ -void line_copy(char to[], char from[]) { - int i; - - i = 0; - while ((to[i] = from[i]) != '\0') - ++i; -} - -/* - * REVERSE_COPY - * Copy FROM into TO (of length LENGTH) in reverse order; assume TO is big - * enough. - */ -void reverse_copy(char to[], char from[], int length) { - int i; - - for (i = 0; i < length; ++i) { - to[length - 1 - i] = from[i]; - } - - to[length] = '\0'; -} - diff --git a/too_long.c b/too_long.c deleted file mode 100644 index dcc6ed4..0000000 --- a/too_long.c +++ /dev/null @@ -1,39 +0,0 @@ -#include - -#define MAXLENGTH 1000 - -int get_line(char line[], int maxlength); -void copy(char to[], char from[]); - -/* - * MAIN - * Print all lines larger than 80 characters. - */ -main() { - int len; // current line length - char line[MAXLENGTH]; // current input line - - while ((len = get_line(line, MAXLENGTH)) > 0) - if (len > 80) { - printf("%s", line); - } - return 0; -} - -/* - * GET_LINE - * Read STDIN into LINE up to MAXLENGTH and return its length. - * Returns the length of a the line. - */ -int get_line(char line[], int maxlength) { - int c, i; - - for (i = 0; i < maxlength - 1 && (c = getchar()) != EOF && c != '\n'; ++i) - line[i] = c; - if (c == '\n') { - line[i] = c; - ++i; - } - line[i] = '\0'; - return i; -} diff --git a/wc.c b/wc.c deleted file mode 100644 index 4a5007d..0000000 --- a/wc.c +++ /dev/null @@ -1,29 +0,0 @@ -#include - -#define IN 1 // inside a word -#define OUT 0 // outside a word - -/* - * Count lines, words, and characters in input. - */ -main() { - int c, nl, nw, nc, state; - - state = OUT; - nl = nw = nc = 0; - - while ((c = getchar()) != EOF) { - ++nc; - - if (c == '\n') - ++nl; - if (c == ' ' || c == '\n' || c == '\t') - state = OUT; - else if (state == OUT) { - state = IN; - ++nw; - } - } - - printf("%d %d %d\n", nl, nw, nc); -} diff --git a/whitespace_converter.c b/whitespace_converter.c deleted file mode 100644 index 6fee3bd..0000000 --- a/whitespace_converter.c +++ /dev/null @@ -1,18 +0,0 @@ -#include - -/* - * Convert concurrent blanks to single blank. - */ -main() { - int c; - - while ((c = getchar()) != EOF) { - if (c == ' ') { - while ((c = getchar()) == ' ') - ; - putchar(' '); - } - - putchar(c); - } -} diff --git a/whitespace_counter.c b/whitespace_counter.c deleted file mode 100644 index ea441e2..0000000 --- a/whitespace_counter.c +++ /dev/null @@ -1,22 +0,0 @@ -#include - -/* - * Count whitespace in input - */ -main() { - int c, n_whitespace; - - n_whitespace = 0; - - while ((c = getchar()) != EOF) { - if (c == '\n') - ++n_whitespace; - if (c == '\t') - ++n_whitespace; - if (c == ' ') - ++n_whitespace; - } - - - printf("%d\n", n_whitespace); -} diff --git a/whitespace_translator.c b/whitespace_translator.c deleted file mode 100644 index efb52b2..0000000 --- a/whitespace_translator.c +++ /dev/null @@ -1,26 +0,0 @@ -#include - -/* - * Translate invisible whitespace characters to visible - * representations. - */ -main() { - int c; - - while ((c = getchar()) != EOF) { - if (c == 8) { - printf("\\b"); - continue; - } - if (c == 9) { - printf("\\t"); - continue; - } - if (c == '\\') { - printf("\\\\"); - continue; - } - - putchar(c); - } -} diff --git a/word_hist.c b/word_hist.c deleted file mode 100644 index 965c1b9..0000000 --- a/word_hist.c +++ /dev/null @@ -1,24 +0,0 @@ -#include - -/* - * Prints a histogram of the lengths of words in its input. - */ -main() { - int c; - int count; - - while ((c = getchar()) != EOF) { - if (c == '\n' || c == '\t' || c == ' ') { - - while (count > 0) { - printf("%s", "*"); - --count; - } - - if (count > 0) { - printf("\n"); - } - } else - ++count; - } -} -- cgit v1.2.3