title: How can I separate numbers and letters in a C++ String?
body: <p>I'm having several input strings containing numbers and letters. Sometimes the space is missing. I would like to add an additional Space each time the string changes from numbers to letters or from letters to numbers.</p>
  
  <p>Example inputs:</p>
  
  <pre><code>"30EinsteinStreet"
  "548 Roe Drive5500 TestCity"
  "44B SaarlouisDrive1234Testtown"
  </code></pre>
  
  <p>they should become:</p>
  
  <pre><code>"30 EinsteinStreet"
  "548 Roe Drive 5500 TestCity"
  "44 B SaarlouisDrive 1234 Testtown"
  </code></pre>
  
  <p>My existing function is not working and I think far to complex. Can anyone provide an easy solution? Preferably using modern C++11 classes but no Boost. Also I'm using GCC so all the regex stuff doesn't work for me.</p>
  
  <p>Thanks</p>
  
  <p>Here is my existing method:</p>
  
  <pre><code>        inline string separateAlphanumChunks(const std::string&amp; s) 
          {
              string ret = "";
              const int sl = s.length();
              //int inserts = 0;
  
              if (sl&lt;=4)
                  return s;
  
              for (int i=0 ; i&lt; sl ; i++)
              {
  //              cerr &lt;&lt; "separateAlphanumChunks: '" &lt;&lt; ret &lt;&lt; "'" &lt;&lt;endl;
                  // check if index would go out of range
                  if (i+4 &gt; sl)
                  {
                      ret += s.substr (i,sl-i);
                      //TODO add the remain to ret
                      break;
                  }
  
                  // seperate chars
                  const char c0 = s[i+0];
                  const char c1 = s[i+1];
  
                  // check if 0 and 1 are the same class
                  const bool c0c = isCharAnInt (c0);
                  const bool c1c = isCharAnInt (c1);
                  bool class0 = false;
                  if (c0c == c1c)
                  {
                      class0 = c0c;
                  }
                  else
                  {
                      ret += c0;
  //                  cerr &lt;&lt; "cont1: '" &lt;&lt; c0 &lt;&lt; "'" &lt;&lt;endl;
                      continue;
                  }
  
                  // seperate chars
                  const char c2 = s[i+2];
                  const char c3 = s[i+3];
  
                  // check if 2 and 3 are the same class
                  const bool c2c = isCharAnInt (c2);
                  const bool c3c = isCharAnInt (c3);
                  bool class2 = false;
                  if (c2c == c3c)
                  {
                      class2 = c2c;
                  }
                  else
                  {
                      ret += c0;
  //                  cerr &lt;&lt; "cont2: '" &lt;&lt; c0 &lt;&lt; "'" &lt;&lt;endl;
                      continue;
                  }
  
                  // check if the 2 classes are different
                  if (class0 != class2)
                  {
                      // split!
                      ret += c0+(c1+string(" ")+c2)+c3;
                      //inserts++;
                      i+=3;
                  }
                  else
                  {
                      ret += c0;
  //                  cerr &lt;&lt; "cont3: '" &lt;&lt; c0 &lt;&lt; "'" &lt;&lt;endl;
                      continue;
                  }
  
              }
  
              // remove double spaces
              //replaceStringInPlace(ret, "  "," "); 
              //cerr &lt;&lt; "separateAlphanumChunks: '" &lt;&lt; ret &lt;&lt; "'" &lt;&lt;endl;
              return ret;
          }
  
      inline bool isCharAnInt (char c)
      {
          //TODO might be able to use isdigit() here
          int i = c - '0';
          return ((i&gt;=0) &amp;&amp; (i&lt;=9));
      }
  </code></pre>