title: C++ Counting Numbers in a String
body: <p>Given a string such as "121", I am trying to count the number of 1's and 2's and then return the string "2112" (because there are 2 one's and 1 two). I am not sure where the problem is in my code but I am getting ridiculous results, if anyone could point out where it goes wrong that would be great. This is my code so far:  </p>
  
  <p>It works now, thank you very much. </p>
  
  <pre><code>string operate(string s) 
  {  
      string input = "121; 
      int count[10] = {0}; 
      string answer; 
  
      for(int n = 0; n &lt; input.length(); n++) 
      {              
          int a = (input[n]-'0');        
          count[a]++;
      }      
  
      for(int n = 1; n &lt; 10; n++) 
      { 
          if(count[n] &gt; 0)   
          {  
              stringstream ss;
              ss &lt;&lt; count[n] &lt;&lt; n;
              answer.append(ss.str());  
          }          
      }         
      return answer;
  }
  </code></pre>