-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_1-21.c
executable file
·128 lines (101 loc) · 2.63 KB
/
exercise_1-21.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <string.h>
/* A program that replaces spaces with tabs so that the text is displayed
* at the proper tab stops. Assumme tab stops every n columns.
*
* Question: Should n be a variable or symbolic parameter?
*/
#define TABSTOP 4 /* Tabs every x columns */
#define MAXLINE 100 /* maximum input line size */
int mygetline(char line[], int maxline);
void entab(char to[], char from[]);
void test(char expected[], char input[] ) {
char actual[MAXLINE];
entab(actual, input);
int result;
result = strcmp(expected, actual);
if (result != 0) {
printf("Expected: %s\n but got %s\n", expected, actual);
}
}
test_main() {
test("hello", "hello");
test(" tab", " tab");
test("a tab at start", "a tab at start");
test("b tab at start", "b tab at start");
test("c tab at start", "c tab at start");
}
main() {
int len; /* current line length */
char line[MAXLINE]; /* current input line */
char current[MAXLINE]; /* current line to print */
while ((len = mygetline(line, MAXLINE)) > 0) {
entab(current, line);
printf("%s", current);
}
return 0;
}
/* mygetline: read a line into s (up to lim characters), return length */
int mygetline(char s[], int lim) {
int c, i;
/* count all the characters but newline. Put all we can (lim) into s */
for (i=0; (c=getchar()) != EOF && c!='\n'; i++) {
if (i<lim) {
s[i] = c;
}
}
/* We stopped. Get the newline */
if (c=='\n') {
if (i<lim) {
s[i] = c;
}
i++;
}
/* add the null terminator to s */
if (i<lim) {
s[i] = '\0';
} else {
/* ran out of space, cut it off */
s[lim-1] = '\0';
}
return i;
}
/* entab: copy 'from' into 'to'. Remove spaces and add tabs to shift to the
next tab stop. Assume 'to' is big enough. */
void entab(char to[], char from[]) {
int i, j, spacestart;
/* haven't seen a space yet */
spacestart = -1;
/* start copying from the beginning */
i = j = 0;
while (from[i] != '\0') {
/* printf("from[i]: %c, i: %d, j: %d, spacestart: %d\n", from[i], i, j, spacestart); */
/* copy the character */
to[j] = from[i];
if (from[i] == ' ' && spacestart < 0) {
/* starting some spaces */
spacestart = j;
}
if ((spacestart >= 0) && (j - spacestart > 1) && ((i % TABSTOP) == 0)) {
/* in a line of spaces, just hit a tabstop */
j = spacestart;
to[j] = '\t';
j++;
to[j] = from[i];
if (from[i] == ' ') {
spacestart = j;
} else {
spacestart = -1;
}
}
if (from[i] != ' ' && spacestart >= 0) {
/* ending some spaces */
spacestart = -1;
}
/* increment the indicies */
i++;
j++;
}
/* wrap it up */
to[j] = '\0';
}