-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3.2_switch.c
More file actions
57 lines (49 loc) · 1.09 KB
/
3.2_switch.c
File metadata and controls
57 lines (49 loc) · 1.09 KB
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
/* Ex. 3.2
* Switch statements to replace newline and tab characters
* with their visual equivalent (\n, \t), as it copies a string.
*/
#include <stdio.h>
#define MAXLINE 1000
/* Converts characters like newline and tab into
* visible escape sequences like \n and \t
* as it copies the string src into dst.
*/
void print_escape(char* dst, char* src)
{
int i = 0, j = 0;
while (src[i] != '\0'){
switch (src[i]){
case '\t':
dst[j++] = '\\';
dst[j++] = 't';
break;
case '\n':
dst[j++] = '\\';
dst[j++] = 'n';
break;
default:
dst[j++] = src[i];
break;
}
i++;
}
dst[j] = '\0';
}
void my_getline(char *s)
{
int i, c = 0;
for (i = 0; i < MAXLINE && ((c = getchar()) != EOF); i++)
{
s[i] = c;
}
s[i] = '\0';
}
int main()
{
char s[MAXLINE];
char d[MAXLINE];
my_getline(s);
print_escape(d, s);
printf("Original s: %s\n", s);
printf("This is d: %s\n", d);
}