-
Notifications
You must be signed in to change notification settings - Fork 1
/
detab.c
87 lines (74 loc) · 1.54 KB
/
detab.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>
#define MAXLINE 1000
#define TABSTOP 8
#define TEST 0
int my_getline(char line[], int len);
int replace_tabs(char s[], int len);
int insert_char(char newChar, char s[], int index, int len);
/* replace strings of blanks by the minimum number of tabs
* and blanks to achieve the same spacing */
int main()
{
int len, newlen;
char line[MAXLINE];
while ((len = my_getline(line, MAXLINE)) > 0) {
newlen = replace_tabs(line, len);
printf("%s\n", line);
printf("original: %d encoded: %d\n\n", len, newlen);
}
}
/* my_getline: read a line into s, return length */
int my_getline(char s[], int len)
{
int c, i;
for(i=0; (c=getchar())!=EOF && c!='\n'; ++i) {
if(i<len-1) {
s[i] = c;
}
}
if(c == '\n') {
s[i] = c;
++i;
}
// do we need an ellipse?
if (i > len - 1) {
s[len - 4] = '.';
s[len - 3] = '.';
s[len - 2] = '.';
s[len - 1] = '\0';
} else {
s[i] = '\0';
}
return i;
}
int replace_tabs(char s[], int len)
{
int i;
i = 0;
while(i < len && len < MAXLINE) {
if(s[i] == '\t') {
s[i] = ' ';
while(++i % TABSTOP != 0 && len < MAXLINE) {
len = insert_char(' ', s, i, len);
}
/* printf("--------------\n"); */
/* printf("%s\n", s); */
} else {
i++;
}
}
return len;
}
int insert_char(char newChar, char s[], int index, int len)
{
int i;
char last, next;
last = newChar;
for(i = index; i <= len; i++) {
next = s[i];
s[i] = last;
last = next;
}
s[++len] = '\0';
return len;
}