-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path20.c
62 lines (58 loc) · 2.1 KB
/
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
#include <stdio.h>
/**
* The exercise asks: "Should n be a variable or a symbolic parameter?"
*
* IMHO, it should be a symbolic constant (in the pdf is written symbolic
* parameter) because its value is known at compile time and this value
* won't change throughout program execution. If it was in a variable, there
* would be the need to access memory during execution to get TABSTOP value.
*/
#define TABSTOP 4
/**
* A program 'detab' that replaces tabs in the input with the proper number of
* blanks to space to the next tab stop. Basically, there is a tabstop every Nth
* position in the input string and all we need to do is expand output with
* whitespaces until the next tabstop every time a tab character (\t) is found
* within length of the current tabstop. The concept of a tabstop makes more
* sense in the context of typewriters and robust text editor (for alignment).
*
* For more info on tabstops check: https://en.wikipedia.org/wiki/Tab_stop
*/
main()
{
int c, i = 0, j = 0;
while (1) {
/* Read until next TABSTOP or EOF. */
for (i = 0; i < TABSTOP && (c = getchar()) != EOF; i++) {
/**
* If a tab char is found, we need to print whitespaces until next tabstop.
*/
if (c == '\t') {
for (j = (TABSTOP - i); j > 0; j--) {
printf(" ");
}
/**
* We are back at the beginning of a tabstop (set to -1 because
* we are inside a for loop and will be updated).
*/
i = -1;
} else {
printf("%c", c);
if (c == '\n') {
/**
* The counter is reset after a newline (set to -1 because
* we are inside a for loop and will be updated).
*/
i = -1;
}
}
}
/**
* Break out of main loop if EOF has been reached.
* (Continue if TABSTOP was reached).
*/
if (c == EOF) {
break;
}
}
}