2

So I want to find a word "ha" from my string(str) and replace it with "wk" from another string(str2) like this:

    #include <iostream>
    #include <conio.h>
    #include <string>
    using namespace std;

int main ()
{
    string str;
    cin>>str;
    string str2("ha");
    while (str.find(str2) != std::string::npos) {
    str.replace(str.find(str2),str2.length(),"wk");
    cout << str << endl;
        }

    return 0;
}

But the problem is that i can't make it work when i start the var1 with another word like "lol haha". not working when not haha at first[1]

Thank You :)

2
  • Beware using namespace std;: stackoverflow.com/questions/1452721 Commented Mar 4, 2018 at 10:23
  • 1
    your problem is with your input, get it with std::getline Commented Mar 4, 2018 at 10:23

1 Answer 1

2

operator>> for std::string only reads until it finds a whitespace character. You probably want std::getline instead:

getline( std::cin, str );
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sorry for asking this, i know this is stupid. But idk how to do that, could you help me again ? Thank you very much.
sorry didnt know you can click on it, my bad. i thought it was blue cause it was code XD. Thank you
Thank you it works as i expected now. Thank you very much :)

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.