aboutsummaryrefslogtreecommitdiff
path: root/1/16.c
blob: f276e88dd06d18a8d5aa414e2eb5e50fe4507b0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
	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.
	===
*/

#include <stdio.h>

#define MAX 1000	/* max line length */

/* read line into string, l, of length size, s, return length */
int readline(char l[], int s) {
	int c, i;
	for (i=0; i<s-1 && (c=getchar())!=EOF && c!='\n'; i++)
		l[i]=c;	
	if (c=='\n') l[i++]=c;
	l[i]='\0';
	return i;
}

/* copy string1, s1, to string2, s2 */
void copy (char s1[], char s2[]) {
	int i=0;
	while ((s2[i]=s1[i])!='\0') i++;
}

int main () {
	int l, m, c;				/* length, max, character */
	char line[MAX], lngst[MAX];	/* line, longest */

	m=0;
	while (l=readline(line, MAX)) {		/* for each line */
		if (line[l-1]!='\n')			/* if line exceeds buffer */
			while ((c=getchar())!=EOF && c!='\n')
				l++;					/* count remaining characters */
		if (l>m) {						/* if longest */
			m=l; copy(line, lngst);		/* record it */
		}
	}
	if (m)							/* if lines were processed, print longest */
		printf("%d: %s\n", m-1, lngst);	
}