Blob


1 /*
2 * 3-2. Write a function escape(s,t) that converts characters like newline
3 * and tab into visible escape sequences like \n and \t as it copies the
4 * string t to s. Use a switch. Write a function for the other direction as
5 * well, converting escape sequences into the real characters.
6 */
8 #include <stdio.h>
10 void escape(char s[], char t[]);
12 main()
13 {
14 char str[400];
15 escape (str, "This\t\t\t is a test\n to see\n\t\t if escaping works");
16 printf("%s\n", str);
17 }
19 void escape(char s[], char t[]) {
20 int i, j;
21 for (i = 0, j = 0; t[j] != '\0'; ++i, ++j) {
22 switch (t[j]) {
23 case '\t':
24 s[i++] = '\\';
25 s[i] = 't';
26 break;
27 case '\n':
28 s[i++] = '\\';
29 s[i] = 'n';
30 break;
31 default:
32 s[i] = t[j];
33 break;
34 }
35 }
36 s[i] = t[j];
37 }