Path: blob/master/languages/cprogs/Ex_3.2_escape.c
1240 views
/* 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 for1the other direction as well,converting the escape sequences into the real characters */23#include<stdio.h>4#define MAXLINE 10005int mgetline(char line[],int maxline);6void escape(char s[],char t[]);789int main(void)10{11char s[MAXLINE],t[MAXLINE];1213mgetline(t,MAXLINE);1415escape(s,t);1617printf("%s",s);1819return 0;20}2122void escape(char s[],char t[])23{24int i,j;2526i=j=0;2728while(t[i] != '\0')29{30switch(t[i])31{32case '\t':33s[j]='\\';34++j;35s[j]='t';36break;37case '\n':38s[j]='\\';39++j;40s[j]='n';41break;42default:43s[j]=t[i];44break;45}46++i;47++j;48}4950s[j]='\0';51}5253int mgetline(char s[],int lim)54{55int i,c;5657for(i=0;i<lim-1 && (c=getchar())!=EOF;++i)58s[i]=c;5960s[i]='\0';61}626364