天天看點

K&R C Exercise 1-22 Solution

/*
 * Exercise 1-22 Write a program to "fold" long input lines into
 * two or more shorter lines after the last non-blank character 
 * that occurs before the n-th column of input. Make sure your 
 * program does something intelligent with very long lines, and 
 * if there are no blanks or tabs before the specified column.
 * 
 * fduan, Dec. 12, 2011
 */

#include <stdio.h>

#define MAX_COL 10

int main()
{
	int c, i;
	char line[MAX_COL + 1] = { '\0' };
	
	i = 0;
	while( ( c = getchar() ) != EOF )
	{
		line[i++] = c;
		if( i == MAX_COL )
		{
			line[i--] = '\0';
			while( line[i] == ' ' || line[i] == '\t' )
				--i;
			line[i+1] = '\0';
			printf( "%s\n", line );
			i = 0;
		}
	}

	return 0;
}