"C" string substitution space: Implement a function that replaces each space in a string with "%20"

Source: Internet
Author: User
  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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.