K&R Solutions - 1.16

Q: 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.

A:

...
int main(void)
{
  int len;
  int max;
  char line[MAXLINE];
  char longest[MAXLINE];

  max = 0;
  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (len > max) {
      max = len;
      copy(longest, line);
    }
    // Following tradition, we will be ignoring the null character that marks
    // the end of every string. The standard library function strlen() also
    // ignores null characters.
    printf("Length: %d\n", len - 1);
    printf("Line: %s\n", line);
  }
  if (max > 0) {
    printf("%s", longest);
  }

  return 0;
}
...
hi
Length: 2
Line: hi

there this is an 
Length: 16
Line: there this is an

arbitrarily long string
Length: 23
Line: arbitrarily long string

moo 
Length: 3
Line: moo

arbitrarily long string