Replace a character in a function

Ok so here's a question I got. I want to replace a single character in a string lets say it's '+'. The string is "five + five". How exactly should i do that? I know i need a four loop but I'm confused about how to use std::replace.

1
2
3
4
5
6
string swag" five + five";
for (int i = 0; i< swag.length(); i++)
{


}


I would appreciate any help :D.
1
2
3
4
5
6
7
8
9
10
std::string dwag = "five + five" ;

// replace the first occurrence of '+' with '*'

// step 1: locate the position of the first '+'
// http://www.cplusplus.com/reference/string/string/find/
const auto pos = dwag.find( '+' ) ; 

// step 2: if the '+' was found, replace it with '*'
if( pos != std::string::npos ) dwag[pos] = '*' ;
1
2
3
4
5
6
7
8
9
10
// replaces the first  '+' with '*'
string swag = " five + five";
for (size_t i = 0; i < swag.length(); i++)
{
   if (swag[i] == '+')
   {
      swag[i] = '*';
      break;
   } 
}
Topic archived. No new replies allowed.