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.
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;
}