From: Steven Baltakatei Sandoval Date: Wed, 14 Sep 2022 13:29:29 +0000 (+0000) Subject: feat(src/kr/ch1/s1.9):Finish exercise e1-16 (longest line) X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2023-05.git/commitdiff_plain/505b2c6f58fbc4d9f17d9541542cfc24fed9b47d?ds=sidebyside feat(src/kr/ch1/s1.9):Finish exercise e1-16 (longest line) --- diff --git a/src/kr_exercises/ch1/s1.9/e1-16..longest_line.c b/src/kr_exercises/ch1/s1.9/e1-16..longest_line.c new file mode 100644 index 0000000..38b7258 --- /dev/null +++ b/src/kr_exercises/ch1/s1.9/e1-16..longest_line.c @@ -0,0 +1,76 @@ +#include + +/* Exercise 1-16. Revise the main routine of the longest-line program + so it will correctly print the length of arbitrarily long input + lines, and as much as possible of the text. */ + +/* Strat: Modify mygetline() to read a char and increment `i` even if + `i 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) /* there was a line */ + printf("%03d:%s", max, longest); + return 0; +}; + +/* mygetline: read a line into s, return length v2 */ +int mygetline(char s[], int lim) { + int c, i; + + for (i=0; (c=getchar())!=EOF && c!='\n'; ++i) { + if (i +#define MAXLINE 1000 /* maximum input line size */ + +int mygetline(char line[], int maxline); /* 'my-' to avoid name collision */ +void copy(char to[], char from[]); + +/* print longest input line */ +int main() { + int len; /* current line length */ + int max; /* maximum length seen so far */ + char line[MAXLINE]; /* current input line */ + char longest[MAXLINE]; /* longest line saved here */ + + max = 0; + while ((len = mygetline(line, MAXLINE)) > 0) + if (len > max) { + max = len; + copy(longest, line); + } + if (max > 0) /* there was a line */ + printf("%s", longest); + return 0; +}; + +/* mygetline: read a line into s, return length */ +int mygetline(char s[], int lim) { + int c, i; + + for (i=0; i>Represents the character. >Represents an (e.g. - is the character<\footnote> + is the character)<\footnote> See . - ). + . >Represents a number (e.g. - is the character>). + is the character)>. + + >Represents the character (i.e. a + with value )<\footnote> + See . + A sequence of @@ -411,6 +416,7 @@ > > > + > > > > @@ -426,6 +432,7 @@ > > > + > > > > @@ -446,21 +453,20 @@ > > > - > + > > heap|?>> machine-independent|?>> operand|?>> > - > + > struct|?>> > > > - > - |prog-language||font-family||\\x>|hh>|?>> - > + > + > > > >