From 9706f875156e5ce4c08444489b634c54f4d62b52 Mon Sep 17 00:00:00 2001 From: Adam Carpenter <53hornet@gmail.com> Date: Tue, 14 May 2019 13:30:53 -0400 Subject: Finished 1.16 --- char_hist.c | 30 ++++++++++++++++++++++++++++++ charhist.c | 30 ------------------------------ longest_line.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ num_digits.c | 29 +++++++++++++++++++++++++++++ numdigits.c | 29 ----------------------------- one_word.c | 16 ++++++++++++++++ oneword.c | 16 ---------------- word_hist.c | 24 ++++++++++++++++++++++++ wordhist.c | 24 ------------------------ 9 files changed, 157 insertions(+), 99 deletions(-) create mode 100644 char_hist.c delete mode 100644 charhist.c create mode 100644 longest_line.c create mode 100644 num_digits.c delete mode 100644 numdigits.c create mode 100644 one_word.c delete mode 100644 oneword.c create mode 100644 word_hist.c delete mode 100644 wordhist.c diff --git a/char_hist.c b/char_hist.c new file mode 100644 index 0000000..312df76 --- /dev/null +++ b/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/charhist.c b/charhist.c deleted file mode 100644 index 312df76..0000000 --- a/charhist.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/longest_line.c b/longest_line.c new file mode 100644 index 0000000..58f2a72 --- /dev/null +++ b/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/num_digits.c b/num_digits.c new file mode 100644 index 0000000..c25fc29 --- /dev/null +++ b/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/numdigits.c b/numdigits.c deleted file mode 100644 index c25fc29..0000000 --- a/numdigits.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 new file mode 100644 index 0000000..8d50542 --- /dev/null +++ b/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/oneword.c b/oneword.c deleted file mode 100644 index 8d50542..0000000 --- a/oneword.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/word_hist.c b/word_hist.c new file mode 100644 index 0000000..965c1b9 --- /dev/null +++ b/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/wordhist.c b/wordhist.c deleted file mode 100644 index 965c1b9..0000000 --- a/wordhist.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