关闭

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

标签: Exercise 1-21
814人阅读 评论(0) 收藏 举报
分类:
/* Write the program entab that replaces strings of blanks by the minimum 
   number of tabs and blanks to achieve the same spacing. Use the same tab 
   stops as for detab. When either a tab or a single blank would suffice to  
   reach a tab stop, which should be given preference? */

#include <stdio.h>

#define TABINC 8        /* tab increment size */

/* replace strings of blanks with tabs and blanks */
main()
{
	int c, nb, nt, pos;
	nb = 0;             /* # of blanks necessary */
	nt = 0;             /* # of tabs necessary */
	for(pos = 1; (c = getchar()) != EOF; ++pos)
	{
		if(c == ' ')
		{
			if(pos % TABINC != 0)
			{
				++nb;   /* increment # of blanks */
			}
			else
			{
				nb = 0; /* reset # of blanks */
				++nt;   /* one more tab */
			}
		}
		else
		{
			for(; nt > 0; --nt)
			{
				putchar('\t'); /* output tab(s) */
			}
			if(c == '\t')      /* forget the blank(s) */
			{
				nb = 0;
			}
			else               /* output blank(s) */
			{
				for(; nb > 0; --nb)
				{
					putchar(' ');
				}
			}
			putchar(c);
			if(c == '\n')
			{
				pos = 0;
			}
			else if(c == '\t')
			{
				pos = pos + (TABINC - (pos - 1) % TABINC) - 1;
			}
		}
	}
}
1
0

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