Blob


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