-
Notifications
You must be signed in to change notification settings - Fork 113
/
04.unescape.c
58 lines (50 loc) · 802 Bytes
/
04.unescape.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
#include <stdio.h>
#define MAX_LEN 1024
void unescape(char[], char[]);
main()
{
char t[] = "Hello\\tWorld!!!\\r\\nEOF\\v";
printf("%s\n", t);
char s[MAX_LEN] = "";
unescape(s, t);
printf("%s\n", s);
}
void unescape(char s[], char t[])
{
int i, j, n;
for (i = 0, j = 0; t[j] != '\0'; i++, j++) {
if (t[j] == '\\' && t[j+1] != '\0') {
j++;
switch(t[j]) {
case 'a':
s[i] = '\a';
break;
case 'b':
s[i] = '\b';
break;
case 'f':
s[i] = '\f';
break;
case 'n':
s[i] = '\n';
break;
case 'r':
s[i] = '\r';
break;
case 't':
s[i] = '\t';
break;
case 'v':
s[i] = '\v';
break;
default:
s[i++] = '\\';
s[i] = t[j];
break;
}
} else {
s[i] = t[j];
}
}
s[i] = '\0';
}