07. Remove All Occurrences of a Substring

Remove Spaces

Link

string replaceSpaces(string &str){
  int pos = string::npos;

  while((pos = str.find(' ')) != string::npos) 
    str.replace(pos, 1, "@40");
  
  return str;
}

Remove Substring

Link

string removeOccurrences(string s, string part) {
    int pos = string::npos;
    while((pos = s.find(part)) != string::npos) 
        s.erase(pos, part.length());
    return s;
}

Last updated