#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * escape_string_for_json(char *str) {
// allocate the length of str
char *nstr = calloc(strlen(str) + 1, sizeof(char));
// loop through each character
long unsigned int c = 0;
long unsigned int d = 0;
while (c < strlen(str)) {
printf("character: %c\n", str[c]);
// json needs everything from '\x00' to '\x1f' escaped
if (str[c] == '"' || str[c] == '\\' || ('\x00' <= str[c] && str[c] <= '\x1f')) {
printf("\tescaping %c\n", str[c]);
// add the escape character to nstr
nstr[d] = '\\';
// increment d to account for the extra space
d++;
// allocate that space in the nstr pointer
nstr = realloc(nstr, d);
// add the character
nstr[d] = str[c];
} else {
// add the character to nstr
nstr[d] = str[c];
}
c++;
d++;
}
// add the \0 at the end
nstr[d] = '\0';
return nstr;
}
int main( int argc, char *argv[] ) {
printf("string to escape: \"%s\"\n", argv[1]);
char *str;
str = escape_string_for_json(argv[1]);
printf("escaped string: \"%s\"\n", str);
free(str);
}
gcc pgrm.c
./a.out "string to \" escape"