3
3
* input with the proper number of blanks to space to the next tab stop. Assume
4
4
* a fixed set of tab stops, say every n columns. Should n be a variable or a
5
5
* symbolic parameter.
6
+ *
6
7
* By Faisal Saadatmand
7
8
*/
8
9
9
10
/*
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.
15
14
*/
16
15
17
16
#include <stdio.h>
21
20
22
21
/* functions */
23
22
int getLine (char [], int );
24
- void detab (void );
25
-
26
- char line [MAXLINE ]; /* currently read line */
27
- char modLine [MAXLINE ]; /* modified line */
28
23
29
24
/* getLine function: read a line into s, return length */
30
25
int getLine (char s [], int lim )
@@ -42,31 +37,29 @@ int getLine(char s[], int lim)
42
37
}
43
38
44
39
/* detab function: replaces tabs with the proper number of blanks */
45
- void detab (void )
40
+ void detab (char line [], char modLine [] )
46
41
{
47
42
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 */
50
45
51
- for (i = 0 ; line [i ] != '\0' ; ++ i )
46
+ for (i = j = 0 ; line [i ] != '\0' ; ++ i )
52
47
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 ];
63
53
modLine [j ] = '\0' ;
64
54
}
65
55
66
56
int main (void )
67
57
{
58
+ char line [MAXLINE ]; /* currently read line */
59
+ char modLine [MAXLINE ]; /* modified line */
60
+
68
61
while (getLine (line , MAXLINE ) > 0 ) {
69
- detab ();
62
+ detab (line , modLine );
70
63
printf ("%s" , modLine );
71
64
}
72
65
return 0 ;
0 commit comments