tags:

views:

141

answers:

1

I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.

+20  A: 

You can use std::replace from <algorithm> instead of using string::replace from <string>

Sample code

#include <iostream>
#include <algorithm>

int main()
{
   std::string s = "I am a string";
   std::replace(s.begin(),s.end(),' ',',');
   std::cout<< s;

} 

Output : I,am,a,string

Prasoon Saurav
+1 for a straight-forward solution that makes my custom built STD::replace functions a waste.
Alexander Rafferty