String space substitution

Source: Internet
Author: User

Title Description

Write a method that replaces all spaces in the string with "%20". Assume that the string has enough space to hold the new characters, and that the true length of the string (less than or equal to 1000) is known, and that the string is made up of uppercase and lowercase letters.

Given a string inistring as the original string, and the length of the string int len, returns the replaced string.

Test examples:

"Mr John Smith" 13
return: "Mr%20john%20smith"
"Hello World  ", 12
return: "Hello%20%20world"
C + + Style solution
Method One: Use String::iterater it to traverse the entire string, and then (*it) "Switch to '%20 '
Method Two: Use String::find () to find the "" in the string, and then use String::replace () to "Switch to"%20 "
#include <fstream> #include <iostream>using namespace Std;class replacement_by_iterator {//Fayi: AC public:st    Ring Replacespace (string inistring, int length) {if (length<=0) return 0;    String strnew;    String::iterator it; For (It=inistring.begin (); It<=inistring.end (); it++) {if (*it== ')//Note single quote: Point to a single character strnew        + = "%20";         else strnew + = (*it);    } return strnew; }};class Replacement_by_replace {//Law II: acpublic:string replacespace (string inistring, int length) {Stri        ng strnew = inistring; while (Strnew.find (")! = Std::string::npos) {//npos:string class defines NPOs as guaranteed to be greater than any valid underlying value.          That is, the maximum size that the string class can hold Strnew.replace (Strnew.find (""), 1, "%20");    } return strnew; }};int Main (int argc, char** argv) {ifstream fin ("test.txt", Ios::in), if (Fin.fail ()) {cout << "File open failed" << Endl ; exit (-1);} Char ch;string teststring;while (fin.get (ch)) {teststring + = CH;} Fin.close ();  Replacement_by_iterator Replace1;   Law one replacement_by_replace replace2;    Law two cout << replace1.replacespace (teststring, Teststring.length ()) << Endl;        cout << replace2.replacespace (teststring, Teststring.length ()); return 0;}

String Space substitution

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.