K&R C Exercise 1-20 Solution

/*
 * Exercise 1-20 Write a program detab that replaces tabs in the input
 * with the proper number of blanks to space to the next tab stop. Assume
 * a fixed set of tab stops, say every n columns.
 *
 * fduan, Dec. 11, 2011 
 */
#include <stdio.h>

#define TAB_LEN 8

int main()
{
	int c, pos, n_blank;
	
	pos = 1;

	while( ( c = getchar() ) != EOF )
	{
		if( c == '\t' )
		{
			n_blank = TAB_LEN - pos + 1;
			while( n_blank-- > 0 )
				putchar( '*' );
			pos = 1;
		}
		else if( c == '\n' )
		{
			putchar( c );
			pos = 1;
		}
		else
		{
			putchar( c );
			++pos;
			pos = ( pos > TAB_LEN ) ? 1 : pos;
		}
	}

	return 0;
}


你可能感兴趣的:(c,input,tabs)