How to remove all occurrences of a word from string in C++

1 Answer

0 votes
#include <string>
#include <iostream>

using namespace std;

void removeWord(string &s, string& word) { 
  string::size_type len = word.length();
  
  for (string::size_type pos = s.find(word); pos != string::npos; pos = s.find(word))
      s.erase(pos, len + 1);
}

int main() {

  string s = "go c c++ csharp go python java go";
  string word = "go";

  removeWord(s, word);
  
  cout << s;
}



/*
run:

c c++ csharp python java 

*/

 



answered Jul 9, 2020 by avibootz

Related questions

3 answers 175 views
1 answer 141 views
2 answers 84 views
2 answers 89 views
1 answer 82 views
2 answers 107 views
...