-
Notifications
You must be signed in to change notification settings - Fork 0
/
302.c
57 lines (50 loc) · 1.36 KB
/
302.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
/* Exercise 3-2. Write a function escape(s,t) that converts characters like newline and tab
into 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 into the real characters. */
void escape(char s[], char t[])
{
int i, j;
char c;
i = j = 0;
while ((c = t[j++]) != '\0'){
switch (c){
case '\n':
s[i++] = '\\';
s[i++] = 'n';
break;
case '\t':
s[i++] = '\\';
s[i++] = 't';
break;
default:
s[i++] = c;
break;
}
}
s[i] = '\0'; //terminate s
}
void real(char s[], char t[])
{
int i, j;
char c1, c2;
for (i = j = 0; c2 = t[j+1], (c1 = t[j]) != '\0' ; j++){
switch (c1){
case '\\':
switch (c2){
case 'n':
s[i++] = '\n';
j++;
break;
case 't':
s[i++] = '\t';
j++;
break;
}
break;
default:
s[i++] = c1;
break;
}
}
s[i] = '\0'; //terminate s
}