aboutsummaryrefslogtreecommitdiff
path: root/ch3/3-02_escape.c
blob: 25bfa347f7ddcb425f2c039b8683682d39861bfc (plain)
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
#include <stdio.h>

/* The C Programming Language: 2nd Edition
 *
 * 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.
 *
 * Answer: Fun! Switches can be used as shorthand if-else chains.
 */

#define STRMAX 80

void escape(char s[], char t[]) {
	// i for t, j for s
	int i, j;
	j = 0;
	for (i = 0; t[i] != '\0'; i++) {
		switch (t[i]) {
			case '\n':
				s[j++] = '\\';
				s[j++] = 'n';
				break;
			case '\t':
				s[j++] = '\\';
				s[j++] = 't';
				break;
			default:
				s[j++] = t[i];
				break;
		}
	}
	s[j] = '\0';
}

void unescape(char s[], char t[]) {
	// i for t, j for s
	int i, j;
	j = 0;
	for (i = 0; t[i] != '\0'; i++) {
		if (t[i] == '\\') {
			switch (t[i + 1]) {
				case 'n':
					s[j++] = '\n';
					break;
				case 't':
					s[j++] = '\t';
					break;
			}
		} else {
			s[j++] = t[i];
		}
	}
	s[j] = '\0';
}

int main() {
	char src[STRMAX] = "foo\nbar\t\tbaz\nnozzle";
	char dest[STRMAX] = "";
	/* Let's escape it... */
	escape(dest, src);
	printf("%s\n", dest);
	/* ...and unescape it! */
	unescape(dest, src);
	printf("%s\n", dest);
	return 0;
}