1 5dc21454 2023-07-14 jrmu /* 1-16. Revise the main routine of the longest-line program so it will
2 5dc21454 2023-07-14 jrmu * correctly print the length of arbitrarily long input lines, and as much
3 5dc21454 2023-07-14 jrmu * as possible of the text.
6 5dc21454 2023-07-14 jrmu /* MAXLINE has been reduced for easier testing */
7 5dc21454 2023-07-14 jrmu #include <stdio.h>
8 5dc21454 2023-07-14 jrmu #define MAXLINE 10
10 5dc21454 2023-07-14 jrmu int getlin(char line[], int maxline);
11 5dc21454 2023-07-14 jrmu void copy(char to[], char from[]);
15 5dc21454 2023-07-14 jrmu int len; /* current line length */
16 5dc21454 2023-07-14 jrmu int max; /* maximum length seen so far */
17 5dc21454 2023-07-14 jrmu char line[MAXLINE]; /* current input line */
18 5dc21454 2023-07-14 jrmu char longest[MAXLINE]; /* longest line saved here */
21 5dc21454 2023-07-14 jrmu while ((len = getlin(line, MAXLINE)) > 0)
22 5dc21454 2023-07-14 jrmu if (len > max) {
24 5dc21454 2023-07-14 jrmu copy(longest, line);
26 5dc21454 2023-07-14 jrmu if (max > 0) /* there was a line */
27 5dc21454 2023-07-14 jrmu printf("%d chars: %s", max, longest);
31 5dc21454 2023-07-14 jrmu int getlin(char s[], int lim)
35 5dc21454 2023-07-14 jrmu for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
37 5dc21454 2023-07-14 jrmu if (c == '\n') {
41 5dc21454 2023-07-14 jrmu s[i] = '\0';
42 5dc21454 2023-07-14 jrmu while ((c=getchar())!=EOF && c!='\n')
47 5dc21454 2023-07-14 jrmu void copy(char to[], char from[])
51 5dc21454 2023-07-14 jrmu while ((to[i] = from[i]) != '\0')