title: Splitting a String in C++ (using cin)
body: <p>I'm doing <a href="http://uva.onlinejudge.org/index.php?option=com_onlinejudge&amp;Itemid=8&amp;page=show_problem&amp;problem=1234" rel="nofollow">THIS</a> UVa problem, which takes in the following input:</p>
  
  <pre><code>This is fun-
  ny!  Mr.P and I've never seen
  this ice-cream flavour
  before.Crazy eh?
  #
  This is fun-
  ny!  Mr.P and I've never seen
  this ice-cream flavour
  before.Crazy eh?
  #
  </code></pre>
  
  <p>and produces this output:</p>
  
  <pre><code>1 1
  2 3
  3 2
  4 3
  5 3
  6 1
  7 1
  8 1
  
  1 1
  2 3
  3 2
  4 3
  5 3
  6 1
  7 1
  8 1
  </code></pre>
  
  <p>In the input, # divides the cases. I'm supposed to get the length of each word and count the frequency of each different length (as you see in the output, a word of length 1 occurs once, length 2 occurs three times, 3 occurs twice, and so on).</p>
  
  <p>My problem is this: When reading in cin, <code>before.Crazy</code> is counted as one word, since there is no space dividing them. It should then be as simple as splitting the string on certain punctuation (<code>{".",",","!","?"}</code> for example)...but C++ seems to have no simple way to split the string.</p>
  
  <p>So, my question: How can I split the string and send in each returned string to my function that handles the rest of the problem?</p>
  
  <p>Here's my code:</p>
  
  <pre><code>int main()
  {
      string input="";
      while(cin.peek()!=-1)
      {   
          while(cin &gt;&gt; input &amp;&amp; input!="#")
          {
              lengthFrequency(input);
              cout &lt;&lt; input &lt;&lt; " " &lt;&lt; input.length() &lt;&lt; endl;
          }
  
          if(cin.peek()!=-1) cout &lt;&lt; endl;
          lengthFrequencies.clear();
      }
      return 0;
  }
  </code></pre>
  
  <p><code>lengthFrequency</code> is a <code>map&lt;int,int&gt;</code>.</p>