Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_3.2_escape.c
1240 views
1
/* 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
2
the other direction as well,converting the escape sequences into the real characters */
3
4
#include<stdio.h>
5
#define MAXLINE 1000
6
int mgetline(char line[],int maxline);
7
void escape(char s[],char t[]);
8
9
10
int main(void)
11
{
12
char s[MAXLINE],t[MAXLINE];
13
14
mgetline(t,MAXLINE);
15
16
escape(s,t);
17
18
printf("%s",s);
19
20
return 0;
21
}
22
23
void escape(char s[],char t[])
24
{
25
int i,j;
26
27
i=j=0;
28
29
while(t[i] != '\0')
30
{
31
switch(t[i])
32
{
33
case '\t':
34
s[j]='\\';
35
++j;
36
s[j]='t';
37
break;
38
case '\n':
39
s[j]='\\';
40
++j;
41
s[j]='n';
42
break;
43
default:
44
s[j]=t[i];
45
break;
46
}
47
++i;
48
++j;
49
}
50
51
s[j]='\0';
52
}
53
54
int mgetline(char s[],int lim)
55
{
56
int i,c;
57
58
for(i=0;i<lim-1 && (c=getchar())!=EOF;++i)
59
s[i]=c;
60
61
s[i]='\0';
62
}
63
64