• Register
1 vote
103 views

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:
6 5 3
6,930 points

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.
9 7 4
38,600 points

Related questions

0 votes
1 answer 20 views
20 views
Convert seconds to hours, minutes and seconds #include <cstdio> #include <iostream> using namespace std; int main() {      int seconds, hours, minutes;      cin >> seconds;      hours = seconds/3600;      cout << seconds << "seconds is equivalent to" << ... know how to fix this? Whenever I do a number less than 3600, a message appears on the screen stating that the program has stopped working.
asked Aug 27 sasha 1.2k points
0 votes
2 answers 459 views
459 views
Problem : I have following question complete the method definition to output the hours given minutes. output for sample program: 3.5
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 10 views