Skip to content

Commit b577619

Browse files
committed
simplified code
1 parent b95235f commit b577619

File tree

1 file changed

+17
-24
lines changed

1 file changed

+17
-24
lines changed

chapter01/1-20.c

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
* input with the proper number of blanks to space to the next tab stop. Assume
44
* a fixed set of tab stops, say every n columns. Should n be a variable or a
55
* symbolic parameter.
6+
*
67
* By Faisal Saadatmand
78
*/
89

910
/*
10-
* Answer: it is wiser to use a symbolic parameter for the value n rather than
11-
* a global variable. The value of n should remain constant throughout the
12-
* program, for a change in n would break algorithm in functions that depend on
13-
* a specific value of n. If need be, it is better to change the value of n in
14-
* a function through a local variable instead.
11+
* Answer: n should be a symbolic parameter, for the value of n should remain
12+
* constant throughout the duration of the program. A change in n would break
13+
* the algorithm.
1514
*/
1615

1716
#include <stdio.h>
@@ -21,10 +20,6 @@
2120

2221
/* functions */
2322
int getLine(char [], int);
24-
void detab(void);
25-
26-
char line[MAXLINE]; /* currently read line */
27-
char modLine[MAXLINE]; /* modified line */
2823

2924
/* getLine function: read a line into s, return length */
3025
int getLine(char s[], int lim)
@@ -42,31 +37,29 @@ int getLine(char s[], int lim)
4237
}
4338

4439
/* detab function: replaces tabs with the proper number of blanks */
45-
void detab(void)
40+
void detab(char line[], char modLine[])
4641
{
4742
int i; /* index for read line */
48-
int j = 0; /* index for modified (written) line */
49-
int toTabStop; /* number of blanks to the next tab stop */
43+
int j; /* index for modified (written) line */
44+
int blanksToTabStop; /* number of blanks to the next tab stop */
5045

51-
for (i = 0; line[i] != '\0'; ++i)
46+
for (i = j = 0; line[i] != '\0'; ++i)
5247
if (line[i] == '\t') {
53-
toTabStop = N - (j % N);
54-
while (toTabStop > 0) {
55-
modLine[j] = ' ';
56-
++j;
57-
--toTabStop;
58-
}
59-
} else {
60-
modLine[j] = line[i];
61-
++j;
62-
}
48+
blanksToTabStop = N - (j % N);
49+
while (blanksToTabStop-- > 0)
50+
modLine[j++] = ' ';
51+
} else
52+
modLine[j++] = line[i];
6353
modLine[j] = '\0';
6454
}
6555

6656
int main(void)
6757
{
58+
char line[MAXLINE]; /* currently read line */
59+
char modLine[MAXLINE]; /* modified line */
60+
6861
while (getLine(line, MAXLINE) > 0) {
69-
detab();
62+
detab(line, modLine);
7063
printf("%s", modLine);
7164
}
7265
return 0;

0 commit comments

Comments
 (0)