aboutsummaryrefslogtreecommitdiff
path: root/ch1/1-16_longest-line.c
blob: 3c865707b8515608d2425e80c421b527ade6a337 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>

/* The C Programming Language: 2nd Edition
 *
 * 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.
 *
 * Answer: The key to arbitrary limits is buffering. Using a buffer allows you
 * to tackle a problem in chunks of memory instead of all at once. It's
 * slightly more complicated, but adds usefulness to a program.
 *
 * This solution follows the spec exactly, by only modifying main(), unlike many
 * other solutions on the internet.
 *
 * Assumptions:
 * 1. "arbitrarily long" is interpreted to mean upto the maximum size of an
 * integer on the given architechture, e.g. 2^32 unsigned on a 32-bit machine.
 * Indeed a string of that size would be larger than 4 gigabytes. It is possible
 * to deal with numbers larger than that, but it involves a great deal of work
 * abstracting away the concept of an integer, similar to dynamically sized
 * arrays.
 *
 * 2. EOF signal (Ctrl-D) must be given on an empty line
 */

/* demonstrate that we can handle lines much greater than this number*/
#define BUFFSIZE 5

int getlinelen(char line[], int maxline);
void copy(char to[], char from[]);

/* print longest input line */
main() {
	/* len of current line, max len seen so far, templen of buffer */
	int len, max, templen;
	char buffer[BUFFSIZE];
	max = len = 0;
	while ((templen = getlinelen(buffer, BUFFSIZE)) > 0) {
		len += templen;
		if (buffer[templen - 1] == '\n') {
			if (len > max) {
				max = len;
			}
			len = 0;
		}
	}
	/* The length returned includes the '\n' character. */
	printf("\nLen of longest line: %d\n", max);
	return 0;
}

/* getlinelen: read a line into s, return length */
int getlinelen(char s[], int lim) {
	int c, i;
	for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
		s[i] = c;
	}
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\0';
	return i;
}


/* copy: copy 'from' into 'to'; assume 'to' is big enough */
void copy(char to[], char from[]) {
	int i;
	i = 0;
	while ((to[i] = from[i]) != '\0') {
		++i;
	}
}