Replacing spaces with% 20 in C

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; /* * Replace spaces with its hex %20 * It will be converted back to space in the actual processing * They make the application segfault in strtok_r() */ 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.

+4
source share
6 answers

Processing strings in C can be a daunting task. I would suggest first going through the line, counting the spaces, and then highlighting a new line of the appropriate size (the original size of the line + (number of spaces * 2)). Then scroll through the source line by pointing to the pointer (or index) on the position in the new line as well as in the original. (Why two pointers? Because every time you come across a space, a pointer to a new line will contain two characters in front of the pointer in the old.)

Here is the code that should do the trick:

 int new_string_length = 0; for (char *c = qry; *c != '\0'; c++) { if (*c == ' ') new_string_length += 2; new_string_length++; } char *qstr = malloc((new_string_length + 1) * sizeof qstr[0]); char *c1, *c2; for (c1 = qry, c2 = qstr; *c1 != '\0'; c1++) { if (*c1 == ' ') { c2[0] = '%'; c2[1] = '2'; c2[2] = '0'; c2 += 3; }else{ *c2 = *c1; c2++; } } *c2 = '\0'; 
+15
source
 qstr[i] = '%'; qstr[i + 1] = '2'; qstr[i + 2] = '0'; 

This line writes three characters to the output buffer, so the next character you write should be written to qstr [i + 3]. However, you only step 1, so the next character is written to qstr [i + 1], overwriting "2".

You will need to keep separate indexes to go through qry and qstr .

+6
source

I agree with David.

It is advisable to do this in two steps: in the first cycle, you simply count the spaces:

 int spaceCounter=0; const int sourceLen = strlen(qry); for(int i = 0; i < sourceLen; ++i) if ( qry[i] == ' ') ++spaceCounter; char* newString = (char*)malloc(sourceLen + 3*spaceCounter*sizeof(char) + 1) //check for null! for(int i = 0; i < sourceLen; ++i) if ( qry[i] == ' ') { *newString++ = '%'; *newString++ = '2'; *newString++ = '0'; } else *newString++ = qry[i]; *newString = '\0'; 

Warning: code not verified.

+2
source

You assign the use of the same counter I, you need to have 2 counters, since the lines have different lengths

your else case assigns qstr [i] = qry [i]; after you wrote% 20, you are at least 2 in the result line.

+1
source

This is called url encoding. You can refer to this page to see a similar implementation: http://www.geekhideout.com/urlcode.shtml

0
source
 char* toHexSpace(const char *s) { char *b=strcpy(malloc(3*strlen(s)+1),s),*p; while( p=strchr(b,' ') ) { memmove(p+3,p+1,strlen(p)); strncpy(p,"%20",3); } return b; } 

required "free" in the context of the call.

0
source

All Articles