-
Notifications
You must be signed in to change notification settings - Fork 0
/
detab-1-20.c
143 lines (107 loc) · 2.28 KB
/
detab-1-20.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
int detabtab(char s[], int len, int pos, int nexttab);
void detab(char s[], int len, int n);
int min(int a, int b);
void prettyprint(char s[], int len);
void should(char is[], char should[]);
main()
{
char s1[100] = "johnny\tate\tlunch\n";
char s2[100] = "johnny\tate\tlunch\n";
char s3[100] = "johnny\tate\tlunch\n";
char s4[100] = "johnny\tate\tlunch\n";
char s5[100] = "johnny\tate\tlunch\n";
detab(s1, 100, 4);
should(s1, "johnny__ate_lunch\n");
detab(s2, 100, 5);
should(s2, "johnny____ate__lunch\n");
detab(s3, 100, 6);
should(s3, "johnny______ate___lunch\n");
detab(s4, 100, 7);
should(s4, "johnny_ate____lunch\n");
detab(s5, 100, 8);
should(s5, "johnny__ate_____lunch\n");
return 0;
}
void should(char is[], char should[])
{
int len1;
int len2;
int i;
len1 = 0;
len2 = 0;
for (i = 0; is[i] != '\0'; i++) {
len1++;
printf("exp[%d] = %c\n", i, is[i]);
}
for (i = 0; should[i] != '\0'; i++) {
len2++;
printf("act[%d] = %c\n", i, should[i]);
}
if (len1 != len2) {
printf("\nActual string is not equal to expected string...\n");
printf("(Actual: %d, expected %d)\n\n", len1, len2);
} else {
printf("\nOK, passed!\n");
}
prettyprint(is, len1);
prettyprint(should, len2);
}
int min(int a, int b)
{
return a > b ? b : a;
}
void prettyprint(char s[], int len)
{
int i;
int c;
for (i = 0; i < len; i++) {
if (s[i] == '\t') {
putchar('\\');
putchar('t');
} else if (s[i] == ' ') {
putchar('_');
} else {
putchar(s[i]);
c = s[i];
}
}
if (c != '\n') {
putchar('\\');
putchar('n');
putchar('\n');
}
}
void detab(char s[], int len, int n)
{
int i;
int nexttab;
nexttab = n - 1;
for (i = 0; i < len; i++) {
if (s[i] == '\t') {
printf("nexttab: %d\n", nexttab);
i = detabtab(s, len, i, nexttab);
nexttab = n;
}
nexttab = nexttab ? nexttab - 1 : n - 1;
}
}
/* detab: remove tab at position pos and replace it with
space and adds nexttab spaces after it */
int detabtab(char s[], int len, int pos, int nexttab)
{
char temp[len];
int i, j;
j = 0;
for (i = pos + 1; i < len; i++) {
temp[j++] = s[i];
}
temp[j] = '\0';
for (i = pos; i < min(len, pos + nexttab + 1); i++)
s[i] = ' ';
j = 0;
for (; i < len; i++) {
s[i] = temp[j++];
}
return min(len, pos + nexttab);
}