K&R C Exercise 1-17 Solution

/*
 * Exercise 1-17 Write a program to print all input lines that are
 * longer than 80 characters.
 *
 * fduan, Dec. 11, 2011 
 */
#include <stdio.h>

#define MAX_LEN		1000
#define LONG_LINE	80

int getline( char line[], int max_len );
int copy_line( char to[], char from[] );

int main()
{
	int len;
	char line[MAX_LEN] = { '\0' };

	while( ( len = getline( line, MAX_LEN ) ) > 0 )
	{
		if( len > LONG_LINE )
			printf( "%d, %s", len, line );
	}

	return 0;
}

int getline( char line[], int max_len )
{
	int c, i, j;
	
	i = j = 0;
	while( ( c = getchar() ) != EOF && c != '\n' )
	{
		if( i < max_len - 2 )
			line[j++] = c;
		++i;
	}
	
	if( c == '\n' )
	{
		line[j++] = c; ++i;
	}

	line[j] = '\0';
	return i;
}

你可能感兴趣的:(K&R C Exercise 1-17 Solution)