-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.2.c
61 lines (58 loc) · 1.32 KB
/
3.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
60
61
#include "stdio.h"
#define MAX_LEN 1000
void escape(char s[], char t[]) {
int j = 0;
for (int i=0; t[i] != '\0'; i++) {
switch (t[i]) {
case '\t':
s[j] = '\\';
j++;
s[j] = 't';
break;
case '\n':
s[j] = '\\';
j++;
s[j] = 'n';
break;
default:
s[j] = t[i];
}
j++;
}
}
void unescape(char s[], char t[]) {
int j = 0;
for (int i=0; t[i] != '\0'; i++) {
if (t[i] == '\\' && t[i+1] != '\0') {
switch (t[i+1]) {
case 't':
s[j] = '\t';
j++;
i++;
break;
case 'n':
s[j] = '\n';
j++;
i++;
break;
default:
s[j] = t[i];
j++;
break;
}
} else {
s[j] = t[i];
j++;
}
}
}
int main() {
char s[MAX_LEN];
char s2[MAX_LEN];
char t[] = "abc\td\\mef\tghi\njkl\n";
printf("%s\n", t);
escape(s, t);
printf("%s\n", s);
unescape(s2, s);
printf("%s\n", s2);
}