K&R C Exercise 1-21 Solution

/*
 * Exercise 1-21 Write a program entab that replaces strings of blanks
 * by the minimum number of tabs and blanks to achieve the same spacing.
 *
 * fduan, Dec. 12, 2011 
 */
#include <stdio.h>

#define TAB_LEN 8

int main()
{
	int c, n_blank;
	
	n_blank = 0;

	while( ( c = getchar() ) != EOF )
	{
		if( c == ' ' )
		{
			++n_blank;
			if( n_blank == TAB_LEN )
			{
				putchar( '#' ); /* tab */
				n_blank = 0;
			}
		}
		else if( c == '\n' )
		{
			n_blank = 0;
		}
		else
		{
			while( n_blank > 0 ) 
			{
				--n_blank;
				putchar( '@' ); /* blank */
			}
			putchar( c );
		}
	}
	return 0;
}


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