1

I'm solving the challenge on Reddit here

And I can't find out how to replace a specific text of a string with another one. I managed to find out how to check if that specific text exists, but I'm having problems replacing it with another text or completely removing it from the string (as the challenge requires that).

Here's my code so far:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int law (string a);

int main() {

    string a;
    cin >> a;

    cout << law(a) << endl;

    return 0;
}

int law(string a){
    if (a.find("NOT") != string::npos)
        return a.replace(a.begin(), a.end(), ' ');
    if(a.find("NOT") != string::npos && ((a.find("AND") == string::npos) || (a.find("OR") == string::npos)))
       return a.erase(remove(a.begin(), a.end(), "NOT"), a.end());

}
1
  • Why do you need text replacing algorithms to apply De Morgan's Law transformation to a given boolean expression? Commented Nov 15, 2013 at 19:22

2 Answers 2

3

You could search the substring position, then assign each character of to the original string:

void replace( std::string& input_string , const std::string& searched_string , const std::string& replace_string )
{
    std::size_t replace_pos = input_string.find( searched_string );

    if( replace_pos != std::string::npos )
    {
        std::copy( std::begin( replace_string ) ,
                   std::end( replace_string ) ,
                   std::begin( input_string ) + replace_pos
                 );

        //Or with a simple for loop (Which is what std::copy does):
        for( std::size_t i = 0 ; i < replace_string.size() ; ++i )
            input_string[i + replace_pos}] = replace_string[i];
    }
}

The point of this solution is that there is no reason to use expensive string concatenation at all to replace text.

Sign up to request clarification or add additional context in comments.

2 Comments

What if the length of the text you are replacing != the length of the text you are putting in?
@BrianGradin I had supposed that the replacement text always fits in the original searched string. Of course if thats not true this solution does not work and you need to split and reconcat the string. My point was that not always you should use expensive concatenations.
2

Think of the resulting string having three parts:

  • The beginning part of the original string
  • The replacement text
  • The ending part of the original string

You can get the beginning and ending part of the original string by using string.substr(). The code will look something like this:

string originalString; // given
string partToReplace; // given
string replacementText; // given

int positionOfText = originalString.find(partToReplace);

string resultString = originalString.substr(0, positionOfText);
resultString += replacementText;
resultString += originalString.substr(positionOfText + partToReplace.length());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.