Unbreakable
Unbreakable

Reputation: 8084

String Replacement of Blank character with %20 neglecting all trailing blank spaces

I want to fill blank space with %20. Below code does not compile and gives error at line

new_String.append(url.at(j));`

Code:

void main()
{
    string url = "abcde";
    string new_String;
    for (int j = 0; j < url.length(); ++j)
    {
        if (url.at(j) == ' ')
        {
            new_String.append("%20");
        }
        else
            new_String.append(url.at(j));
    }

With the suggestions given by users of Stackoverflow I am able to execute my program. Below code works just fine. But the code is awkwardly complex (especially the calculation of trailing blank spaces). Can anyone give me some suggestion to make the code more feasible. The program takes input string and replaces blank character with %20 but the blank character in the end should be neglected and should not be replaced by %20 .

#include"iostream"
using namespace std;
#include"string"
void main()
{
    string url = "ta  nuj  ";
    int count = 1; // it holds the blank space present in the end
    for (int i = 0; i < url.length(); i++) // this is written to caculate the blank space that //are at the end
    {
        if (url.at(i) == ' ')
        {
            for (int k = i + 1; k < url.length(); k++)
            {
                if ((int)url.at(k) != 32)
                {
                    count = 1;
                    break;
                }
                else
                {
                    count++;
                    i++;
                }
            }
        }
    }
        string newUrl;

        for (int j = 0; j < url.length()-count; j++)
        {
            if (url.at(j) == ' ')
            {
                newUrl.append("%20");
            }
            else
                newUrl += (url[j]);
        }
        cout << "\nThe replaced url is:" << newUrl;
}

Upvotes: 1

Views: 634

Answers (3)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

Can anyone give me some suggestion to make the code more feasible?

If that is the question, the answer is to use the algorithm functions to do this easily.

First, you should trim the string of trailing spaces. Second, you build a new string by traversing the original string, testing for a space character as you loop.

The following illustrates both of these concepts:

#include <algorithm>
#include <string>
#include <iostream>
#include <cctype>
#include <functional>

using namespace std;

// A function object that will take a character, and append either "%20" or the 
// original character to our new string    
struct Converter
{
    std::string* pS;  // this points to our string we will build

    Converter(std::string* s) : pS(s) {}

    void operator()(char ch) 
    {
        if ( isspace(ch) )
            (*pS) += "%20";   // it's a space, so replace
        else
            *pS += ch;    // it is not a space, so use the same character
    }
};

// trim spaces from end of string
std::string &rtrim(std::string &s) 
{
    s.erase(std::find_if(s.rbegin(), s.rend(), 
            std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    return s;
}

// function to return the new string by looping over the original string 
string getNewString(const std::string& s)
{
    std::string outStr;  // our new string

    // loop over each character, calling our Coverter::operator()(char) function
    for_each(s.begin(), s.end(), Converter(&outStr));
    return outStr;
}

int main()
{
    string instring = "This string has spaces ";
    cout << getNewString(rtrim(instring));
}

Output:

This%20string%20has%20spaces

Live example: http://ideone.com/kXPKgU

To note, this link on SO provides answers to What's the best way to trim std::string?.

The for_each is an algorithm function that basically is a loop. Each iteration of the loop calls the function, function object, or lambda (if using C++11). Since in this case we're using a function object called Converter, we overloaded the operator() of Converter to build up our string.

Upvotes: 0

Dev
Dev

Reputation: 11

Did you try:

newUrl.append(1, url.at(j));

Upvotes: 0

Blindy
Blindy

Reputation: 67380

Opening up a manual will show you that string::append() only takes a string as an input, so you probably want:

new_String += string(url[j]);

As a side note, you don't need all that boilerplate (append, at, the integer loop) in modern C++.

Upvotes: 2

Related Questions