Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,914 questions

51,847 answers

573 users

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 201 views
1 answer 162 views
2 answers 104 views
2 answers 110 views
1 answer 104 views
2 answers 132 views
...