You know you can assign to strings like they are arrays, right? No reason to call .replace for single characters.
Have you considered what happens if your loop starts on line 18? There seems to be no way to increment [tt]i[/ii] in that case. Why are you not simply using a for loop?
#include <string>
#include <iostream>
std::string foo( std::string in ) {
for ( auto & c : in ) {
if ( ! isalnum( c ) ) c = 'X';
}
return in;
}
int main() {
std::string word = "Hello, Dolly!";
word = foo( word );
std::cout << word << '\n';
return 0;
}
HelloXXDollyX
It does not really matter that spaces gets replaced (with spaces).