-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise3-2.c
98 lines (84 loc) · 2.02 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
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
/*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.*/
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000
void reverse(char to[], const char from[]);
void reverse_2(char to[], const char from[]);
void get_line(char s[],int);
int main()
{
char line_from[MAXLINE]="";
char line_to[MAXLINE]="";
get_line(line_from,MAXLINE);
//reverse(line_to,line_from);
//reverse_2(line_to,line_from);
printf("%s\n",line_to);
return 0;
}
void get_line(char s[], int max)
{
int c,i=0;
while( (i<max-1) && (c=getchar())!=EOF )
s[i++]=c;
s[i]='\0';
}
void reverse(char to[], const char from[])
{
int i,j;
for(i=0,j=0; from[i]!='\0';i++)
{
switch(from[i])
{
case '\n':
{
to[j++]='\\';
to[j++]='n';
break;
}
case '\t':
{
to[j++]='\\';
to[j++]='t';
break;
}
default:
{
to[j++]=from[i];
break;
}
}
}
to[j]='\0';
}
void reverse_2(char to[], const char from[])
{
int i,j,k;
for(i=0,j=1,k=0; j<=strlen(from) ;i++,j++)
{
switch(from[i]+from[j])
{
case 92+110://new line \=92 n=110
{
to[k++]=10; // \n=10
i++,j++;
break;
}
case 92+116://new line \=92 t=116
{
to[k++]=9; // \t=9
i++,j++;
break;
}
default:
{
to[k++]=from[i];
break;
}
}
}
to[k]='\0';
}