summaryrefslogtreecommitdiff
path: root/ch01/ex17-print.c
blob: 35bb18a7151ae87af7f02e743a27f40148ff5420 (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
#include <stdio.h>
#define MAXLINE 1000	/* maximum input line size */

int getln(char line[], int maxline);

/* print lines that are longer than 80 characters */
int main()
{
	int len;	/* current line length */
	char line[MAXLINE];	/* current input line */

	while ((len = getln(line, MAXLINE)) > 0)
		if (len > 80)
			printf("%s", line);
	return 0;
}

/* getln:  read a line into s, return length */
int getln(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;
}