"C" string substitution space: Implement a function to replace the space in the string with "%20"

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

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.