Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_1.21_entab.c
1240 views
1
/**
2
*
3
* Exercise 1.21 - Write a Program entab that replaces strings of blanks by
4
* the minimum number of tabs and blanks to achieve the same spacing.
5
*
6
* Use the same tab stops as for detab.
7
*
8
**/
9
10
#include<stdio.h>
11
12
#define TABINC 8
13
14
int main(void)
15
{
16
int nb,nt,pos,c;
17
18
nb = 0;
19
nt = 0;
20
21
for(pos=1;(c=getchar())!=EOF;++pos)
22
if( c == ' ')
23
{
24
if((pos % TABINC) != 0)
25
++nb;
26
else
27
{
28
nb = 0;
29
++nt;
30
}
31
}
32
else
33
{
34
for( ; nt > 0 ; --nt)
35
putchar('\t');
36
if( c == '\t')
37
nb = 0;
38
else
39
for( ; nb > 0; --nb)
40
putchar(' ');
41
42
putchar(c);
43
44
if(c == '\n')
45
pos = 0;
46
else if ( c == '\t')
47
pos = pos + ( TABINC - (pos -1) % TABINC) - 1;
48
}
49
50
return 0;
51
}
52
53