I'm attempting to parse a string. What I need to do is have a function turn any instance of apostrophes (') or quotation marks (") and put a backslash(\) in front of them. Like this:
From:
I can't hear what "you" said.
To:
I can\'t hear what \"you\" said.
Here is my function so far, which is tacking on the needed adjustment to the end of the string, not where I need it to go.
Code:
char *fixQuotes(char *theString)
{
char buf[2500];
int length = 0;
snprintf(buf, sizeof(buf), "%s", theString );
length = ((strlen(buf)));
while(length)
{
if (buf[length] == '\'')
{
strcat(buf, "\\\'");
length--;
continue;
}
if (buf[length] == '\"')
{
strcat(buf, "\\\"");
length--;
continue;
}
length--;
}
return _strdup(buf);
}
So taking the above example string, if I ran it thru this current function, I end up with:
I can't hear what "you" said.\"\"\'
Again, I need it to read:
I can\'t hear what \"you\" said.
Any and all suggestions/criticisms/help would be very much appreciated.