-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpra1-21v2_Solution.c
62 lines (57 loc) · 1.42 KB
/
pra1-21v2_Solution.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>
#define TABSTOP 4
int main(void)
{
size_t spaces = 0;
int ch;
size_t x = 0; /* position in the line */
size_t tabstop = TABSTOP; /* get this from the command-line
* if you want to */
while ((ch = getchar()) != EOF)
{
if (ch == ' ')
{
spaces++;
}
else if (spaces == 0) /* no space, just printing */
{
putchar(ch);
x++;
}
else if (spaces == 1) /* just one space, never print a tab */
{
putchar(' ');
putchar(ch);
x += 2;
spaces = 0;
}
else
{
while (x / tabstop != (x + spaces) / tabstop)
/* are the spaces reaching behind the next tabstop ? */
{
putchar('\t');
x++;
spaces--;
while (x % tabstop != 0)
{
x++;
spaces--;
}
}
while (spaces > 0) /* the remaining ones are real space */
{
putchar(' ');
x++;
spaces--;
}
putchar(ch); /* now print the non-space char */
x++;
}
if (ch == '\n')
{
x = 0; /* reset line position */
}
}
return 0;
}