Page 34

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

#include <stdio.h>

#define TAB_STOP 8

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

	while ((c = getchar()) != EOF) {
		if (c == '\t') {
			for (int i = 0; i < (TAB_STOP - tab_pos); i++) {
				putchar(' ');
			}
			tab_pos = 0;
		} else {
			putchar(c);

			if (c == '\n') {
				tab_pos = 0;
			} else {
				tab_pos++;
				tab_pos %= TAB_STOP;
			}
		}
	}
}