Path: blob/master/languages/cprogs/Ex_1.21_entab.c
1240 views
/**1*2* Exercise 1.21 - Write a Program entab that replaces strings of blanks by3* the minimum number of tabs and blanks to achieve the same spacing.4*5* Use the same tab stops as for detab.6*7**/89#include<stdio.h>1011#define TABINC 81213int main(void)14{15int nb,nt,pos,c;1617nb = 0;18nt = 0;1920for(pos=1;(c=getchar())!=EOF;++pos)21if( c == ' ')22{23if((pos % TABINC) != 0)24++nb;25else26{27nb = 0;28++nt;29}30}31else32{33for( ; nt > 0 ; --nt)34putchar('\t');35if( c == '\t')36nb = 0;37else38for( ; nb > 0; --nb)39putchar(' ');4041putchar(c);4243if(c == '\n')44pos = 0;45else if ( c == '\t')46pos = pos + ( TABINC - (pos -1) % TABINC) - 1;47}4849return 0;50}515253