-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescape.c
84 lines (67 loc) · 1.57 KB
/
escape.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
/* Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab charo visible escape
* sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction
* as well, converting escape sequences charo the real characters. */
#include "stdio.h"
#define MAX 1000
void strscan(char t[]);
void escape(char s[], const char t[]);
void compile(char dst[], const char src[]);
int main(void) {
char t[MAX], s[MAX];
strscan(t);
escape(s, t);
printf("After escape:\n%s\n", s);
compile(t, s);
printf("After recompile\n%s\n", t);
return 0;
}
/* Scan input charo string. */
void strscan(char t[]) {
size_t i;
for (i = 0; (t[i] = getchar()) != EOF; ++i);
}
/* Convert newline and tab character into `\n` and `\t` for visibility. */
void escape(char s[], const char t[]) {
size_t i, j;
j = 0;
for (i = 0; t[i] != '\0'; ++i) {
switch (t[i]) {
case '\n':
s[j++] = '\\';
s[j++] = 'n';
break;
case '\t':
s[j++] = '\\';
s[j++] = 't';
break;
default:
s[j++] = t[i];
break;
}
}
s[j] = '\0';
}
/* Compile `\n` and `\t` */
void compile(char dst[], const char src[]) {
size_t i, j;
j = 0;
for (i = 0; src[i] != '\0'; ++i) {
if (src[i] == '\\') {
switch (src[i + 1]) {
case 'n':
dst[j++] = '\n';
i++;
break;
case 't':
dst[j++] = '\t';
i++;
break;
default:
dst[j++] = src[i];
break;
}
} else
dst[j++] = src[i];
}
dst[j] = '\0';
}