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
|
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 1000
void getstr(char s[]);
void detab(char to[], char from[], int n);
/* detabs input by replacing tabs with specified number of blanks */
int main(int argc, char *argv[]) {
char s[MAXLEN], dtab[MAXLEN];
int tabw = atoi(argv[1]);
printf("Enter text to detab and press ctrl+d\n");
getstr(s);
detab(dtab, s, tabw);
printf("\nTab width: %d\n", tabw);
printf("%s\n", dtab);
return 0;
}
void getstr(char s[]) {
int i, c;
for (i = 0; i < MAXLEN - 1 && (c = getchar()) != EOF; i++)
s[i] = c;
s[i] = '\0';
}
void detab(char to[], char from[], int n) {
int i, j, k;
char c;
for (i = 0, j = 0; j < MAXLEN - 1 && (c = from[i]) != '\0'; i++) {
if (c == '\t') {
for (k = 0; k < n; k++)
to[j++] = ' ';
} else {
to[j++] = from[i];
}
}
to[j] = '\0';
}
|