-1

I have a function that removes all occurrences of a sub-string from a c++ string. But I want it to insert spaces before or after removing every sub-string. My Function if given below:

    void removeSubstrs(string& s, string& p) {
    string::size_type n = p.length();
    for (string::size_type i = s.find(p);
        i != string::npos;
        i = s.find(p))
        s.erase(i, n);
}

For example if I input "AZAZBLACKAZAZBERRYAZ", I want the output "BLACK BERRY"....

1 Answers1

1

if what you want is something like this:

assume "-" as space
input: "AZAZBLACKAZAZBERRYAZ" and "AZ"
output: "--BLACK--BERRY-"

then, this may help you:

  void removeSubstrs(string& s, string& p) {
    string::size_type n = p.length();
    for (string::size_type i = s.find(p); i != string::npos; i = s.find(p))
    {
        s.replace(i, n, 1, ' ');
        //OR
        //s.erase(i, n);
        //s.inset(i, ' '); //Add this line
    }
}
Zhr Saghaie
  • 1,031
  • 2
  • 16
  • 41