-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy path1-20.c
66 lines (56 loc) · 1.43 KB
/
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
/*
* Exercise 1-20. Write a program detab function that replaces tabs in the
* input with the proper number of blanks to space to the next tab stop. Assume
* a fixed set of tab stops, say every n columns. Should n be a variable or a
* symbolic parameter.
*
* By Faisal Saadatmand
*/
/*
* Answer: Here, n should be a symbolic constant, for the value of n
* should remain constant throughout the duration of the program. It could also
* be a const int.
*/
#include <stdio.h>
#define MAXLEN 1000
#define N 4 /* tabstop for every n columns */
/* functions */
int getLine(char [], int);
/* getLine function: read a line into s, return length */
int getLine(char s[], int lim)
{
int c, i;
for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
s[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
/* detab function: replaces tabs with the proper number of blanks */
void detab(char in[], char out[])
{
int i; /* index for read line */
int j; /* index for modified (written) line */
int nblanks; /* number of blanks to the next tab stop */
for (i = j = 0; in[i] != '\0'; ++i)
if (in[i] == '\t') {
nblanks = N - (j % N);
while (nblanks-- > 0)
out[j++] = ' ';
} else
out[j++] = in[i];
out[j] = '\0';
}
int main(void)
{
char in[MAXLEN]; /* currently read line */
char out[MAXLEN]; /* modified line */
while (getLine(in, MAXLEN) > 0) {
detab(in, out);
printf("%s", out);
}
return 0;
}