-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3-2.c
104 lines (85 loc) · 2.74 KB
/
3-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
99
100
101
102
103
/*
Write a function escape (s, t)
that converts characters like new line 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
*/
/*
C has a variety of integer types:
char (at least 8 bits), short (at least 16 bits),
int (at least 16 bits), long (at least 32 bits).
There are unsigned varieties of those.
If you assign a value that is too large to a plain type, the results are undefined
char and int is pretty much the same a part from the six=ze of these
A string can be initialized in different ways.
1. char str[] = "GeeksforGeeks";
2. char str[50] = "GeeksforGeeks";
3. char str[] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. char str[14] = {'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
or just simply assign char stri[] = "lalalalala";
*/
#include <stdio.h>
#include <string.h>
#define MAXNUMBER 100 /* number of array */
void escape_1 (char str[], char strout[]);
void reverse_escape_1 (char str[], char strout[]);
int main (){
char str[] = "ichi \\n want to \\t this to happen ";
printf ("original one is %s \n", str);
char strout [MAXNUMBER];
//escape_1(str, strout);
reverse_escape_1(str, strout);
printf ("converted one is %s \n", strout);
return 0;
}
void escape_1 (char str[], char strout[]){
int i = 0;
int n = strlen(str);
printf ("strlen is %d \n", n);
if ( n > MAXNUMBER)
printf ("the strng is too big \n");
for (int j = 0; j < n; j++){
switch (str[j]){
case '\n':
strout[i++] = '\\';
strout[i++] = 'n';
break;
case '\b':
strout[i++] = '\\';
strout[i++] = 't';
break;
default:
strout[i++] = str [j];
break;
}
}
strout[i] = '\0';
printf ("%c", strout[i]);
}
void reverse_escape_1 (char str[], char strout[]){
int i = 0;
int n = strlen(str);
printf ("strlen is %d \n", n);
if ( n > MAXNUMBER)
printf ("the strng is too big \n");
for (int j = 0; j < n; j++){
switch (str[j]){
case '\\':
j++;
switch (str[j]){
case 'n':
strout[i++] = '\n';
break;
case 't':
strout[i++] = '\t';
break;
}
break;
default:
strout[i++] = str [j];
break;
}
}
strout[i] = '\0';
printf ("%c", strout[i]);
}