How to replace one char by another using std::string in C++? In my case, I'm trying to replace each c character by  character.
I've tried this way :
std::string str = "abcccccd";
str = str.replace('c', ' ');
But, this wouldn't work.
With the std::replace algorithm:
#include <algorithm>
#include <string>
#include <iostream>
int main()
{
  std::string str = "abccccd";
  std::replace(str.begin(), str.end(), 'c', ' ');
  std::cout << str << std::endl;
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With