Page 31

Write a program to print all input lines that are longer than 80 characters.

#include <stdio.h>

#define LOWER_BOUND 80
#define UPPER_BOUND 1000

int get_line(char str[], int lim);

int main()
{
	char line[UPPER_BOUND];
	int len = 0;

	while ((len = get_line(line, UPPER_BOUND)) > 0) {
		/* Subtract one to account for the null terminator */
		if (len - 1 > LOWER_BOUND) {
			printf("%d %s\n", len, line);
		}
	}
	return 0;
}

int get_line(char s[], int lim)
{
	int c, i = 0;

	while ((c = getchar()) != EOF) {
		if (c == '\n') {
			if (i < lim - 1) {
				s[i] = c;
			}
			break;
		} else if (i < lim - 1) {
			s[i] = c;
		}
		i++;
	}

	if (i < lim) {
		s[i] = '\0';
	} else {
		s[lim - 1] = '\0';
	}

	return i;
}