I'm trying to write a function which would convert all escape sequences in a string in their non-printable form. Basically if I have a string "This \n makes a new line", I would like it to be "This makes a new line". So far I've got this. I'm calling from main:
int main()
{
unescape("This \\n\\n is \\t\\t\\t string number \\t 7.");
return 0;
}
char* unescape(char* s)
{
char *esc[2] = {"\\n", "\\t"};
int i;
char* uus = (char*)calloc(80, sizeof(char));
char* uus2 = (char*)calloc(80,sizeof(char));
strncpy(uus, s, strlen(s));
for(i = 0; i < 2; i++)
{
while(strstr(uus, esc[i]) != NULL) //checks if \\n can be found
{
//printf("\n\n%p\n\n", strstr(uus, esc[i]));
int c = strstr(uus, esc[i]) - uus; //gets the difference between the address of the beginning of the string and the location
//where the searchable string was found
uus2 = strncpy(uus2, uus, c); //copies the beginning of the string to a new string
//add check which esc is being used
strcat(uus2, "\n"); //adds the non-printable form of the escape sequence
printf("%s", uus2);
//should clear the string uus before writing uus2 to it
strncpy(uus, uus2, strlen(uus2)); //copies the string uus2 to uus so it can be checked again
}
}
//this should return something in the end.
}
Basically, what I need to do now, is take the part from the string uus after "\n" and add it to the string uus2 so I can run the while loop again. I thought about using strtok but hit a wall as it makes two separate strings using some kind of delimiter which is not always there in my case.
edit: Adding the rest of the string to uus2 should be before strncpy. This is the code without it.
edit vol2: This is the code that works and which I ended up using. Basically edited Ruud's version a bit as the function I had to use had to return a string. Thanks a lot.
char* unescape(char* s)
{
char *uus = (char*) calloc(80, sizeof(char));
int i = 0;
while (*s != '\0')
{
char c = *s++;
if (c == '\\' && *s != '\0')
{
c = *s++;
switch (c)
{
case 'n': c = '\n'; break;
case 't': c = '\t'; break;
}
}
uus[i] = c;
i++;
}
uus[i] = '\0';
return uus;
}
strncpynever copied the trailing\0; (3)\twas replaced by a newline because of the hardcoded\ninstrcat(uus2, "\n");.