title: Replace space with an underscore
body: <p>I am trying to write something that will replace all the spaces in a string with an underscore.</p>
  
  <p>What I have so far.</p>
  
  <pre><code>string space2underscore(string text)
  {
      for(int i = 0; i &lt; text.length(); i++)
      {
          if(text[i] == ' ')
              text[i] = '_';
      }
      return text;
  }
  </code></pre>
  
  <p>For the most part this would work, if I was doing something like.</p>
  
  <pre><code>string word = "hello stackoverflow";
  word = space2underscore(word);
  cout &lt;&lt; word;
  </code></pre>
  
  <p>That would output "hello_stackoverflow", which is just what I want. </p>
  
  <p>However if I was to do something like</p>
  
  <pre><code>string word;
  cin &gt;&gt; word;
  word = space2underscore(word);
  cout &lt;&lt; word;
  </code></pre>
  
  <p>I would just get the first word, "hello".</p>
  
  <p>Does anybody know a fix for this?</p>