String substitution spaces: Implements a function that replaces spaces in a string with "%20" #include <stdio.h> #include <assert.h>void replace (char *src) {assert ( SRC); int oldlen = 0; Original string length int newlen = 0; New string length int blacknum = 0; Number of spaces int newback = 0; New string Trailing int oldback = 0; The original string trailing while (Src[oldlen]! = ' + ') {if (Src[oldlen] = = ') {blacknum++;} oldlen++;} Newlen = Oldlen + blacknum * 2;oldback = OldLen-1; Newback = Newlen-1;while (oldback!=0) {if (src[oldback] = = ') {src[newback--] = ' 0 '; src[newback--] = ' 2 '; src[newback] = '%';} Else{src[newback] = Src[oldback];} oldback--; newback--;}} int main () {char p[20] = "Hello world!"; Replace (p);p rintf ("%s\n", p); return 0;return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C" string substitution space: Implement a function to replace the space in the string with "%20"