Programmers learn & share
+1 vote

Problem :

I am trying to find solution for following error
replace any space ' ' by '_' in 2-character string passcode. sample output for the given program:
by (6.9k points)   | 68 views

1 Answer

0 votes

Solution :

For single character it is easier to use the std::replace algorithm:

std::replace(passCode.begin(), passCode.end(), ' ', '_');

If you can not use the algo header you can use your own replace function. 

It can be done with a simple loop:

template<typename Iterator, typename T>
void replace(Iterator begin, Iterator end, const T& old_val, const T& new_val)
    for (; begin != end; ++begin)
        if (*begin == old_val) *begin = new_val;
I followed above approch and my issue got ressolved.
by (36.1k points)  
2,187 questions
2,514 answers
241 users