blob: 57aef6c92ea98568b0afb3bf87cb33b099a4aaa8 (
plain) (
blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include <stdio.h>
#define MAXLEN 5000
char* escape(char to[], const char from[]);
char* etr(char to[], const char from[]);
int
main(void)
{
int i;
char input;
char str[MAXLEN];
char e[MAXLEN];
char r[MAXLEN];
for (i = 0; (input = getchar()) != EOF && i+1 < MAXLEN; ++i)
str[i] = input;
str[i] = '\0';
if (str[i-1] != '\n')
printf("\n");
printf("%s\n", escape(e, str));
printf("%s\n", etr(r, e));
return 0;
}
char*
escape(char to[], const char from[])
{
int i, j;
for (i = j = 0; from[i] != '\0' && j+1 < MAXLEN; ++i)
switch (from[i]) {
case '\n' :
to[j++] = '\\';
to[j++] = 'n';
break;
case '\t' :
to[j++] = '\\';
to[j++] = 't';
break;
case '\a' :
to[j++] = '\\';
to[j++] = 'a';
break;
case '\b' :
to[j++] = '\\';
to[j++] = 'b';
break;
case '\f' :
to[j] = '\\';
to[j++] = 'f';
break;
case '\r' :
to[j++] = '\\';
to[j++] = 'r';
break;
case '\v' :
to[j++] = '\\';
to[j++] = 'v';
break;
default:
to[j++] = from[i];
break;
}
to[i] = '\0';
return to;
}
char*
etr(char to[], const char from[])
{
int i, j;
for (i = j = 0; from[i] != '\0'; ++i) {
if (from[i] == '\\')
switch (from[++i]) {
case 'n' :
to[j++] = '\n';
break;
case 't' :
to[j++] = '\t';
break;
case 'a' :
to[j++] = '\a';
break;
case 'b' :
to[j++] = '\b';
break;
case 'f' :
to[j++] = '\f';
break;
case 'r' :
to[j++] = '\r';
break;
case 'v' :
to[j++] = '\r';
break;
default :
to[j++] = '\\';
to[j++] = from[i];
break;
}
else
to[j++] = from[i];
}
to[j] = '\0';
return to;
}
|