I am trying to write something that will replace all the spaces in a string with an underscore.
What I have so far.
string space2underscore(string text)
{
for(int i = 0; i < text.length(); i++)
{
if(text[i] == ' ')
text[i] = '_';
}
return text;
}
For the most part this would work, if I was doing something like.
string word = "hello stackoverflow";
word = space2underscore(word);
cout << word;
That would output "hello_stackoverflow", which is just what I want.
However if I was to do something like
string word;
cin >> word;
word = space2underscore(word);
cout << word;
I would just get the first word, "hello".
Does anybody know a fix for this?
std::
or not. (Though technically, your code works withusing std::cout;
at the top) – Aerophone