-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise3-2.c
65 lines (62 loc) · 1.07 KB
/
exercise3-2.c
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
#include <stdio.h>
void escape(char t[], char s[]);
void convEscapes(char t[], char s[]);
int main(){
char testIn[] = "Hello\nWorld\tHow\tDoes\tit\tfeel\t";
char testOut[50];
char testOut2[50];
printf("Test data:\n%s\n\n",testIn);
escape(testIn, testOut);
printf("Escape test:\n%s\n\n",testOut);
convEscapes(testOut, testOut2);
printf("convEscapes test:\n%s\n",testOut2);
return 0;
}
/* convert tab and newline to visible escape sequences
t is the input, s is the output
*/
void escape(char t[], char s[]){
int i = 0;
int j = 0;
while(t[i] != '\0'){
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;
}
++i;
}
s[j] = '\0';
}
void convEscapes(char t[], char s[]){
int i = 0;
int j = 0;
while(t[i] != '\0'){
switch(t[i]){
case '\\':
if(t[i+1] == 'n'){
s[j++] = '\n';
i++;
}else if(t[i+1] == 't'){
s[j++] = '\t';
i++;
}else{
s[j++] = t[i];
}
break;
default:
s[j++] = t[i];
break;
}
++i;
}
s[j] = '\0';
}