forked from wzqnls/The-C-Programming-Language-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-2.c
More file actions
48 lines (42 loc) · 991 Bytes
/
3-2.c
File metadata and controls
48 lines (42 loc) · 991 Bytes
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
/*
* 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.
* */
#include <stdio.h>
void escape(char s[], char t[]);
int main()
{
char s[] = "asdfasdf\nasdf\tasdf\n\tasdf";
char t[1024];
escape(s, t);
printf("%s\n", t);
}
void escape(char s[], char t[])
{
char c;
int i = 0;
int j = 0;
while((c = s[i]) != '\0')
{
switch(c)
{
case '\n':
t[j] = '\\';
j++;
t[j] = 'n';
break;
case '\t':
t[j] = '\\';
j++;
t[j] = 't';
break;
default:
t[j] = s[i];
break;
}
i++;
j++;
}
t[j] = '\0';
}