C language: String Replacement space: implement a function to replace each space in the string with "% 20 ".
From the past to the future: replace the original character based on the original one (of course, the premise is that there is sufficient space). If a space is replaced from the past to the next, it will inevitably overwrite the original character and cannot be implemented. Because the space is replaced with "% 20", and two more characters are replaced each time, the total number of spaces in the string can be calculated, then the size of the new array is "size of the original array + 2 * Space ". Process from the back to the front: When a non-space is encountered, move directly to the back, and replace the space with "% 20" until the position to be inserted pointer and the original array are the pointer coincidence position.
# Include <stdio. h> # include <string. h> int main () {char arr [] = "we are happy. "; int I = 0; int j = 0; int len = 0; int count = 0; len = strlen (arr); for (I = 0; I <len; I ++) {if (arr [I] = '') {count ++ ;}} I = len; j = 2 * count + len; while (I! = J & I> = 0) {if (arr [I] = '') {arr [j --] = '0 '; arr [j --] = '2'; arr [j --] = '%'; I --;} else {arr [j] = arr [I]; j --; I --;} len = strlen (arr); for (I = 0; I <len; I ++) {printf ("% c ", arr [I]);} printf ("\ n"); return 0 ;}