Replace a space in a string with%20

Source: Internet
Author: User

------------------------------------------------------------------------------------------------------

For example: There is a string we are family, the implemented string is we%20are%20family.

If the space is replaced by a space in front and back, then the family will be moved backwards two times; Now,

Set aside enough space first, move the family first, then move is, when the space is filled.

------------------------------------------------------------------------------------------------------

C Language code:

# include <stdio.h># include <stdlib.h># include <string.h>#  include <assert.h># define max 50void insert (char *p) {      assert (P);     char *pstart = null;      char *pend = NULL;     int black = 0;      int size = strlen (P);           pstart = p + size;      while  (*P)                           //statistics reserved Spaces number      {           if  (*p ==  '   ')            {               black++;            }          p ++;     }         pend =  pstart +  (black * 2);         while  ( Pstart < pend)      {           if  (*pstart !=  '   ')            {               *pend =  *pstart;                pstart--;               pend--;           }          else           {                *pend-- =  ' 0 ';                *pend-- =  ' 2 ';                *pend-- =  '% ';                *pstart--;          }      }}int main () {     char str[max] =  " ";           printf (" Please enter string: ");      Gets (str);           insert (str);      printf ("%s\n", str);Nbsp;         system ("pause");      return 0;}

------------------------------------------------------------------------------------------------------

work tip: The loop statement, the longest cycle should be placed in the most inner layer, the shortest in the outermost, in order to reduce the number of CPU cross-cutting cycle layer, it is recommended for loop control variable to take "half open half closed".

------------------------------------------------------------------------------------------------------

This article is from the "Nobody" blog, please be sure to keep this source http://814193594.blog.51cto.com/10729329/1708947

Replace a space in a string with%20

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.