-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtab.c
90 lines (82 loc) · 1.29 KB
/
tab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#define TABLENGTH 8;
int detab(int, int);
void entab(int, int);
int main()
{
int c, i, hasTabbed, spaceCounter;
i=hasTabbed=spaceCounter=0;
while((c=getchar())!=EOF){
/*
// 1-20 detab
if(c=='\t'){
i+= detab(i,hasTabbed);
hasTabbed=1;
} else if(c=='\n'){
hasTabbed=0;
putchar(c);
i=0;
} else{
hasTabbed=0;
putchar(c);
++i;
}
//printf("%d", i);
*/
//1-21 entab
if(c==' '){
if(spaceCounter==0)
spaceCounter = 1;
else
++spaceCounter;
} else if(c=='\n'){
if(spaceCounter>0){
entab(spaceCounter, i);
i+=spaceCounter;
spaceCounter=0;
}
putchar(c);
i=0;
} else {
if(spaceCounter>0){
entab(spaceCounter, i);
i+=spaceCounter;
spaceCounter=0;
}
putchar(c);
++i;
}
}
return 0;
}
int detab(int startingPoint, int first){
int i,j;
i = startingPoint;
i%=TABLENGTH;
//printf("%d", i);
j=0;
while(i< 8&& (i!=0||!first)){
putchar(' ');
++i;
++j;
}
return j;
}
void entab(int numSpace, int startingPoint){
if(numSpace>=8-startingPoint%8 ){
putchar('\t');
if(startingPoint%8==0)
putchar('\t');
numSpace-=8-startingPoint%8;
}
while(numSpace>0){
if(numSpace>8){
putchar('\t');
numSpace-=8;
} else {
putchar(' ');
--numSpace;
}
}
}