string Substitution spaces: Implement a function that replaces each space in a string with "%20". For example, enter "We are happy.", then output "we%20are%20happy." #include <stdio.h> #include <assert.h>char* replace (char* p) {char* ret = p;int num = 0;int Oldlen = 0;int n Ewlen = 0;char* q = p;char* R;assert (P! = NULL), while (*p! = ') ') {if (*p = = ') {num++;} oldlen++;p + +;} p = Q;newlen = Oldlen + 2 * num;q = p + oldlen-1;r = p + newlen-1;while (q! = r) {if (*q = = ') {*r--= ' 0 '; *r--= ' 2 '; *r--= '% ';} Else{*r = *q;r--;} q--;} return ret;} int main () {char p[20] = "We are happy."; printf ("Original string is:%s\n", p);p rintf ("The replaced string is:%s\n", replace (p)); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"C" string substitution space: Implement a function that replaces each space in a string with "%20"