Replaces a space in a string with the specified content--4

Source: Internet
Author: User
Tags first string

Implement a function that replaces all spaces in a string with "%20", such as the input string "How beautiful You are!", and the output should be "how%20beautiful%20you%20are!".


First of all, if you open a space and enough to replace all the blanks with "%20", then a word copy in the past, when the space encountered in the "%20", this inefficient method is certainly feasible, but obviously not only inefficient but also wasted storage space, therefore, To save space is to change the space of the original string, the precondition is that the original string space is sufficient to store the space will be changed to "%20" after the string, because "%20" size than a space to be larger;

You can then think of traversing the string, when the space is encountered, the remaining characters will be moved back strlen ("%20") length, and then put "%20" in, and then backward traversal, such a method, although space-saving, but not high efficiency, because each encounter a space after the character will be moved backward, So the last character moves the number of spaces in the string, that is, a character may have to be moved several times to reach the last position it should be.

Therefore, you can change the way of thinking, is to calculate the last character should be to the position, one-time to move it over, that is, from the back and forward, when the space encountered when the string "%20" into the strings, until the first string to traverse to the original, so that the space is replaced, And each character is moved only once to the place where he should stay;

The code is implemented as follows:

#include  <iostream> #include  <stdio.h> #include  <string.h> #include  < Assert.h>using namespace std;void replacespace (char*& dst, const char* &NBSP;SRC) {    assert (DST&NBSP;&AMP;&AMP;&NBSP;SRC);   //determine the validity of the parameters      char* tmp = dst;    size_t s_count = 0;     while (*tmp !=  ')   //calculates the number of hollow lattice of a string     {            if (* (tmp++)  ==  '   ')              ++s_count;    }        size_t dst_len = strlen (DST);   //string length to be replaced      size_t src_len = strlen (SRC);   //The string length to replace     char * Tail = dst+dst_len;                        //is replaced by the last character of the trailing string '     char *right_place = tail+ ' s_count* (src_len-1)    //the position where the last character should remain after replacement     while (TAIL&NBSP;&GT;=&NBSP;DST )     {           if (*tail !=   '   ')         {             *right_place = *tail;//when a character is not a space, move to a replacement position          }        else         {            right_place -=   (src_len-1)   //The starting position of the beginning copy of the string to be replaced              strncpy (Right_Place, src, src_len);        }         --tail;        --right_place;     }   }int main () {    char *str = new char[ 1024];    cout<< "please input string:";     gets (str);     replacespace (str,  "20%");    cout<< "The string  after replace:  "<<str<<endl;    delete[] str;     return 0;}


To run the program:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/75/wKioL1cfWZ6Rn-05AAAXywW77VY494.png "title=" Stringret.png "alt=" Wkiol1cfwz6rn-05aaaxyww77vy494.png "/>


In fact, I think that if the storage space of the string is set to dynamic capacity, so that there is no space enough to use the situation will not have a one-time space to the waste of the problem, but then the first try to realize that this is not at all, because the string is entered manually, When the input is likely to have exceeded the maximum amount of storage space opened, not to mention the next check capacity ah what.



Finish

This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1768027

Replaces a space in a string with the specified content--4

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.