title: Reverse word in a string
body: <p>This code here is for reversing words in a string. The problem is that it only reverses the first word in the string. When I ran a trace I found that it is stopping after encountering the statement <code>if(s[indexCount] == '\0') break;</code></p>
  
  <p>Why the code is getting null character every time the first word is reversed even though some other character is present after the first word.</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  int main()
  {
      string s;
      char tchar;
      int indexCount=0,charCount=0,wordIndex;
      cin&gt;&gt;s;
      while(1){
          if(s[indexCount]==' ' &amp;&amp; charCount==0) continue;
          if(s[indexCount]==' ' || s[indexCount]=='\0' ){
              wordIndex=indexCount-charCount;
              charCount=indexCount-1;
              while(charCount!=wordIndex &amp;&amp; charCount&gt;wordIndex){
                  tchar=s[wordIndex];
                  s[wordIndex]=s[charCount];
                  s[charCount]=tchar;
                  charCount--;
                  wordIndex++;
              }
              if(s[indexCount] == '\0') break;
              indexCount++; charCount=0;
          }
          else{
              charCount++;
              indexCount++;
          }
      }
      cout&lt;&lt;"\nReveresed words in the string : \n\t"&lt;&lt;s&lt;&lt;endl;
     return 0;
  }
  </code></pre>
  
  <p>Also I'm using <code>while(1)</code>. Does it make this a bad code?</p>