-
Notifications
You must be signed in to change notification settings - Fork 3
/
5-11-entab.c
55 lines (40 loc) · 1.17 KB
/
5-11-entab.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEFAULT_TABSTOP 4
#define MAX_INPUT_SIZE 100
int main(int argc, char **argv) {
char input[MAX_INPUT_SIZE];
int input_index = 0;
input[input_index] = getchar();
while (input[input_index] != '\n') {
input[++input_index] = getchar();
}
input[input_index] = '\0';
int length = strlen(input);
int arg_index = 1;
int tabstop = 0;
if (arg_index > argc - 1)
tabstop = DEFAULT_TABSTOP;
else
tabstop = atoi(argv[arg_index++]);
int index;
int space_counter = 0;
for (index = 0; index < length; index++) {
if (input[index] == '*')
space_counter++;
else
space_counter = 0;
if (space_counter == tabstop) {
index -= tabstop - 1;
length -= tabstop - 1;
input[index] = '\t';
for (int move_index = index + 1; move_index < length; move_index++)
input[move_index] = input[move_index + tabstop - 1];
space_counter = 0;
input[length] = '\0';
}
}
printf("%s", input);
return 0;
}