-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.2.c
73 lines (67 loc) · 1014 Bytes
/
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
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#define NL 10
#define TAB 9
#define BSLASH 92
void escape(char [], char []);
void unescape(char [], char []);
/* 3.2 copy t to s, replacing newlines and tabs with \n and \t */
void
escape(char s[], char t[])
{
int i = 0;
int j = 0;
while (t[i] != '\0') {
switch (t[i]) {
case NL:
i++;
s[j++] = '\\';
s[j++] = 'n';
break;
case TAB:
i++;
s[j++] = '\\';
s[j++] = 't';
default:
s[j++] = t[i++];
break;
}
}
}
void
unescape(char s[], char t[])
{
int i = 0;
int j = 0;
while (t[i] != '\0') {
if (t[i] == '\\') {
switch (t[++i]) {
case 'n':
s[j++] == NL;
i++;
break;
case 't':
s[j++] == TAB;
i++;
break;
default:
s[j++] == '\\';
s[j++] == t[i];
break;
}
} else {
s[j++] = t[i++];
}
}
}
int
main(void)
{
char from[1024] = "This \n is \nmy string";
printf("%s\n", from);
char to[1024] = {0};
escape(to, from);
printf("%s\n", to);
unescape(from, to);
printf("%s\n", from);
return 0;
}