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 :)

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
Michael
  • 29
  • 1
  • 5

1 Answers1

2

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

getline( std::cin, str );
aschepler
  • 70,891
  • 9
  • 107
  • 161