-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise1-21.2.c
59 lines (56 loc) · 1.2 KB
/
exercise1-21.2.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
#include <stdio.h>
#define TAB '\t'
#define TABSPACE 4
#define SPACE ' '
#define MAX 1024
/*
exercise 1-21: write a program 'entab' that replaces strings of spaces with the minimum number of tabs and blanks to achieve the same spacing. use the same number of tabs as 'detab'. When either a tab or blank would suffice to reach a tab stop, which should be given preference?
*/
int main()
{
char oldline[MAX];
char newline[MAX];
int old = 0;
int new = 0;
int countspaces = 0;
int tabs = 0;
int spaces = 0;
while (fgets(oldline, MAX, stdin))
{
for ( old = 0, new = 0; oldline[old] != '\0'; old++)
{
while (oldline[old] == SPACE)
{
// if ( inspace == 0)
// {
// inspace = 1;
// }
++countspaces;
++old;
}
if (countspaces > 0 )
{
tabs = (countspaces / TABSPACE);
spaces = (countspaces % TABSPACE);
while ( tabs > 0 )
{
newline[new++] = TAB;
--tabs;
}
while ( spaces > 0 )
{
newline[new++] = SPACE;
--spaces;
}
}
else
{
newline[new++] = oldline[old];
}
}
newline[new] = '\0';
printf("oldline (%d characters): \t %s \n", old, oldline);
printf("newline (%d characters): \t %s \n", new, newline);
}
return 0;
}