关闭

C - The C Answer (2nd Edition) - Exercise 1-20

标签: Exercise 1-20
661人阅读 评论(0) 收藏 举报
分类:
/* 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. Should n be a variable or a symbolic 
   parameter? */

#include <stdio.h>

#define TABINC 8        /* tab increment size */

/* replace tabs with the proper number of blanks */
main()
{
	int c, nb, pos;
	nb = 0;             /* number of blanks necessary */
	pos = 1;            /* position of character in line */
	while((c = getchar()) != EOF)
	{
		if(c == '\t')   /* tab character */
		{
			nb = TABINC - (pos - 1) % TABINC;
			while(nb > 0)
			{
				putchar(' ');
				++pos;
				--nb;
			}
		}
		else if(c == '\n') /* newline character */
		{
			putchar(c);
			pos = 1;
		}
		else               /* all other characters */
		{
			putchar(c);
			++pos;
		}
	}
}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4278229次
    • 积分:113506
    • 等级:
    • 排名:第6名
    • 原创:271篇
    • 转载:1篇
    • 译文:0篇
    • 评论:70条