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