Muhamed Cicak Muhamed Cicak - 18 days ago 4
C Question

K&R Book 3-2 Exercise (escape function)

"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." Is a Exercise 3-2 from K&R book. So i tried to do it. My escape function works properly. But my inverseescape is giving me the same answer as the escape function is giving. Here is the code:

#include <stdio.h>
#define MCC 100

void espace(int* str,int* t);
void inverseescape(int * str,int *t);
void getline(int*);

int main(void){
int firstarr[MCC];
int secondarr[MCC];
int thirdarr[MCC];
getline(firstarr);
espace(firstarr,secondarr);
printf("-----------AFTER ESCAPE------------\n");
for(int g=0;secondarr[g];g++){
printf("%c",secondarr[g]);
}
inverseescape(secondarr,thirdarr);
printf("\n-----------AFTER INVERSEESCAPE------------\n");
for(int g=0;thirdarr[g];g++){
printf("%c",thirdarr[g]);
}
return 0;
}

void getline(int* arr){
int i=0;
int c=0;
while((c=getchar())!=EOF){
arr[i]=c;
arr[i+1]=0;
i++;
}
}

void espace(int* string,int* target){
int i = 0;
int j = 0;
while(string[i]){
switch(string[i]){
case '\a':
target[j]='\\';
target[++j]='a';
target[++j]=0;
break;
case '\b':
target[j]='\\';
target[++j]='b';
target[++j]=0;
break;
case '\t':
target[j]='\\';
target[++j]='t';
target[++j]=0;
break;
case '\n':
target[j]='\\';
target[++j]='n';
target[++j]=0;
break;
case '\v':
target[j]='\\';
target[++j]='v';
target[++j]=0;
break;
case '\f':
target[j]='\\';
target[++j]='f';
target[++j]=0;
break;
case '\r':
target[j]='\\';
target[++j]='r';
target[++j]=0;
break;
case '\"':
target[j]='\\';
target[++j]='\"';
target[++j]=0;
break;
case '\'':
target[j]='\\';
target[++j]='\'';
target[++j]=0;
break;
case '\?':
target[j]='\\';
target[++j]='\?';
target[++j]=0;
break;
case '\\':
target[j]='\\';
target[++j]='\\';
target[++j]=0;
break;
default:
target[j]=string[i];
target[++j]=0;
break;
}
i++;
}
}

void inverseescape(int* string,int* target){
int i = 0;
while(string[i]){
switch(string[i]){
case '\\':
switch(string[i+1]){
case 'a':
target[i]='\a';
case 'b':
target[i]='\b';
case 't':
target[i]='\t';
case 'n':
target[i]='\n';
case 'v':
target[i]='\v';
case 'f':
target[i]='\f';
case 'r':
target[i]='\r';
case '\"':
target[i]='\"';
case '\'':
target[i]='\'';
case '\?':
target[i]='\?';
case '\\':
target[i]='\\';
}
default:
target[i]=string[i];
}
target[++i]=0;
}
}


I am using switch statement first time (and probably last) so the code is not well-written (i guess).

Input:


shadow

of

the

beast


Output:


-----------AFTER ESCAPE------------

shadow\nof\nthe\nbeast

-----------AFTER INVERSEESCAPE------------

shadow\nof\nthe\nbeast

Answer

You're missing the break in the inverseescape case statement. case's behaviour has the curious property of falling through to the statement below unless you explicitly break out of the case. In this case it's falling through to your default case and setting the output to the input.

Comments