-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_21.c
47 lines (43 loc) · 1.07 KB
/
1_21.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
/* Write a program entab that replaces strings of blanks by the minimum number of tabs and blanks to
achieve the same spacing. Use the same tab stops as for detab. When either a tab or a single blank would
suffice to reach a tab stop, which should be given preference? */
#include <stdio.h>
#define TABSTOP 10
#define TABSPACE 5
#define MAXLINE 1000
main() {
char line[MAXLINE];
int character, i, blanks = 0, tabs, spaces, i_position;
for(i = 0; i <= MAXLINE - 1 && ((character = getchar()) != EOF); i++) {
if (character == ' ') {
blanks++;
}
else if (character == '\t') {
blanks += TABSPACE;
}
else {
if (blanks > 0) {
int blocks_remaining = (i % TABSTOP);
i -= blanks;
if (blanks > blocks_remaining)
blanks -= blocks_remaining;
tabs = blanks / TABSPACE;
spaces = blanks % TABSPACE;
while (tabs > 0) {
line[i] = '\t';
i++;
tabs--;
}
while (spaces > 0) {
line[i] = ' ';
i++;
spaces--;
}
blanks = 0;
}
line[i] = character;
}
}
line[i] = '\0';
printf("%s", line);
}