summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 8e020de4370434566525eed5369c7aef8d5ff81d (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
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
120
121
122
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
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.
*/

int escape(char s[], char t[])
{
int outputPos = 0;
for (int i=0; s[i]!='\0';++i)
	{
	switch(s[i])
		{
		case '\n':
			t[outputPos++] = '\\';
			t[outputPos++] = 'n';
			break;
		case '\t':
			t[outputPos++] = '\\';
			t[outputPos++] = 't';
			break;
		case '\r':
			t[outputPos++] = '\\';
			t[outputPos++] = 'r';
			break;
		case '\\':
			t[outputPos++] = '\\';
			t[outputPos++] = '\\';
			break;
		case '\'':
			t[outputPos++] = '\\';
			t[outputPos++] = '\'';
			break;
		case '\"':
			t[outputPos++] = '\\';
			t[outputPos++] = '\"';
			break;
		case '\v':
			t[outputPos++] = '\\';
			t[outputPos++] = 'v';
			break;
		case '\f':
			t[outputPos++] = '\\';
			t[outputPos++] = 'f';
			break;
		case '\b':
			t[outputPos++] = '\\';
			t[outputPos++] = 'b';
			break;
		case '\a':
			t[outputPos++] = '\\';
			t[outputPos++] = 'a';
			break;
		default:
			t[outputPos++] = s[i];
			break;
		}

	}
t[outputPos] = '\0';//add null char
}

int escapeReverse(char t[], char s[])
{
int outputPos = 0;
for (int i=0; t[i]!='\0';++i)
	{
	switch(t[i])
		{
		case '\\':
			if (t[i+1] == 'n'){s[outputPos++] = '\n'; ++i; break;}
			if (t[i+1] == 't'){s[outputPos++] = '\t'; ++i; break;}
			if (t[i+1] == 'r'){s[outputPos++] = '\r'; ++i; break;}
			if (t[i+1] == 'v'){s[outputPos++] = '\v'; ++i; break;}
			if (t[i+1] == 'f'){s[outputPos++] = '\f'; ++i; break;}
			if (t[i+1] == 'b'){s[outputPos++] = '\b'; ++i; break;}
			if (t[i+1] == 'a'){s[outputPos++] = '\a'; ++i; break;}
			if (t[i+1] == '\''){s[outputPos++] = '\''; ++i; break;}
			if (t[i+1] == '\"'){s[outputPos++] = '\"'; ++i; break;}
			if (t[i+1] == '\\'){s[outputPos++] = '\\'; ++i; break;}
			break;
		default:
			s[outputPos++] = t[i];
			break;
		}

	}
s[outputPos] = '\0';//add null char
}

int main(void)
{
char s[] = "Yes\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tYes\r\nNo\n\'\"\\\vYes\f\b\aNo\n";

int length = strlen(s);

int originalLength = length;

for (int i=0;i<originalLength;++i)
	if (s[i] == '\n'||s[i] == '\t'||s[i] == '\r'||s[i] == '\''||s[i] == '\"'||s[i] == '\v'||s[i] == '\f'||s[i] == '\b'||s[i] == '\a'||s[i] == '\\'){++length;}

char t[length+1];//allocate enough space for the result string including the null char
t[length+1] = '\0';

escape(s, t);
printf("%s\n", t);

escapeReverse(t, s);
printf("%s", s);

return 0;
}