diff options
author | Adam Carpenter <53hornet@gmail.com> | 2019-05-14 13:30:53 -0400 |
---|---|---|
committer | Adam Carpenter <53hornet@gmail.com> | 2019-05-14 13:30:53 -0400 |
commit | 9706f875156e5ce4c08444489b634c54f4d62b52 (patch) | |
tree | 706a30846a7237b4eb05d5acfd840fbeba890c22 | |
parent | e0ac6e6a3c74f0e4d5936462eaae4d450fa3ed43 (diff) | |
download | learning-c-9706f875156e5ce4c08444489b634c54f4d62b52.tar.xz learning-c-9706f875156e5ce4c08444489b634c54f4d62b52.zip |
Finished 1.16
-rw-r--r-- | char_hist.c (renamed from charhist.c) | 0 | ||||
-rw-r--r-- | longest_line.c | 58 | ||||
-rw-r--r-- | num_digits.c (renamed from numdigits.c) | 0 | ||||
-rw-r--r-- | one_word.c (renamed from oneword.c) | 0 | ||||
-rw-r--r-- | word_hist.c (renamed from wordhist.c) | 0 |
5 files changed, 58 insertions, 0 deletions
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<stdio.h> +#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/numdigits.c b/num_digits.c index c25fc29..c25fc29 100644 --- a/numdigits.c +++ b/num_digits.c |