------------------------------------------------------------------------------------------------------
For example: There is a string we are family, the implemented string is we%20are%20family.
If the space is replaced by a space in front and back, then the family will be moved backwards two times; Now,
Set aside enough space first, move the family first, then move is, when the space is filled.
------------------------------------------------------------------------------------------------------
C Language code:
# include <stdio.h># include <stdlib.h># include <string.h># include <assert.h># define max 50void insert (char *p) { assert (P); char *pstart = null; char *pend = NULL; int black = 0; int size = strlen (P); pstart = p + size; while (*P) //statistics reserved Spaces number { if (*p == ' ') { black++; } p ++; } pend = pstart + (black * 2); while ( Pstart < pend) { if (*pstart != ' ') { *pend = *pstart; pstart--; pend--; } else { *pend-- = ' 0 '; *pend-- = ' 2 '; *pend-- = '% '; *pstart--; } }}int main () { char str[max] = " "; printf (" Please enter string: "); Gets (str); insert (str); printf ("%s\n", str);Nbsp; system ("pause"); return 0;}
------------------------------------------------------------------------------------------------------
work tip: The loop statement, the longest cycle should be placed in the most inner layer, the shortest in the outermost, in order to reduce the number of CPU cross-cutting cycle layer, it is recommended for loop control variable to take "half open half closed".
------------------------------------------------------------------------------------------------------
This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1708947
Replace a space in a string with%20