-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path03.escape.c
56 lines (52 loc) · 773 Bytes
/
03.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
#include <stdio.h>
#define MAX_LEN 1000
void escape(char[], char[]);
main()
{
char t[] = "Hello\tWorld\r\n\v";
printf("%s\n", t);
char s[MAX_LEN];
escape(s, t);
printf("%s\n", s);
}
void escape(char s[], char t[])
{
int i, j, n;
for (i = 0, j = 0; t[j] != '\0'; i++, j++) {
switch(t[j]) {
case '\a':
s[i++] = '\\';
n = 'a';
break;
case '\b':
s[i++] = '\\';
n = 'b';
break;
case '\f':
s[i++] = '\\';
n = 'f';
break;
case '\n':
s[i++] = '\\';
n = 'n';
break;
case '\r':
s[i++] = '\\';
n = 'r';
break;
case '\t':
s[i++] = '\\';
n = 't';
break;
case '\v':
s[i++] = '\\';
n = 'v';
break;
default:
n = t[j];
break;
}
s[i] = n;
}
s[i] = '\0';
}