Page 60
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>
#define LEN 100
void escape(char t[], char s[]);
int main()
{
char out[LEN];
escape("This is an example of a newline:\n, and this is an example of a tab:\t",
out);
printf("%s\n", out);
}
void escape(char t[], char s[])
{
int i = 0;
char c;
while ((c = t[i]) != '\0') {
switch (c) {
case '\n':
s[i++] = '\\';
s[i++] = 'n';
break;
case '\t':
s[i++] = '\\';
s[i++] = 't';
break;
default:
s[i++] = c;
}
}
}