I am writing a fastcgi application for my site in C. Do not ask why, leave all this part.
Just help me with this problem - I want to replace the spaces in the query string with% 20. Here's the code I'm using, but I don't see 20 in the output, only%. Where is the problem?
the code:
unsigned int i = 0; char *qstr = NULL; for(i = 0; i <= strlen(qry); i++) { void *_tmp; if(qry[i] == ' ') { _tmp = realloc(qstr, (i + 2) * sizeof(char)); if(!_tmp) error("realloc() failed while allocting string memory (space)\n"); qstr = (char *) _tmp; qstr[i] = '%'; qstr[i + 1] = '2'; qstr[i + 2] = '0'; } else { _tmp = realloc(qstr, (i + 1) * sizeof(char)); if(!_tmp) error("realloc() failed while allocating string memory (not space)\n"); qstr = (char *) _tmp; qstr[i] = qry[i]; } }
In the code, qry has the value char *, as the actual parameter for the function. I tried with I + 3, 4, 5 in realloc () in a space replacement block, without success.
source share