title: Difference between while loop and for loop in C leading to output difference
body: <p>I am writing a code for the function <code>maxCharToFront()</code> that accepts a character string <code>str</code> as parameter, finds the largest character from the string (based on ASCII value), and moves it to the beginning of the string.</p>
  <p>Main function:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  void maxCharToFront(char *str);
  int main()
  {
     char str[80], *p;
     
     printf(&quot;Enter a string: \n&quot;);
     fgets(str, 80, stdin);
     if (p=strchr(str,'\n')) *p = '\0';
     printf(&quot;maxCharToFront(): &quot;);
     maxCharToFront(str);
     puts(str);
     return 0;
  }
  </code></pre>
  <p>This is the function using for loop:</p>
  <pre><code>void maxCharToFront(char *str)
  {
      int i, maxcharint=0, maxindex=0;
      char maxchar;
      for (i=0; str[i] != '\0'; i++){
          
          if ( (int)str[i]&gt; maxcharint){
              maxcharint = (int)str[i];
              maxindex = i;
          }
          i++;
      }
      
      for (i = maxindex-1; i&gt;-1; i--){
          str[i+1]= str[i];
      }
      maxchar = maxcharint;
      str[0]= maxchar;
  }
  </code></pre>
  <p>when I enter ab as input, it gives back ab as output for for loop.</p>
  <p>This is the function using while loop:</p>
  <pre><code>void maxCharToFront(char *str)
  {
      int i=0, maxindex=0, maxcharint=0;
      char maxchar;
      while (str[i]!='\0'){
          if ((int)str[i]&gt;maxcharint){
              maxindex = i;
              maxcharint = (int)str[i];
          }
          i++;
      }
      
      for(i=maxindex-1; i&gt;-1; i--){
          str[i+1]=str[i];
      }
      maxchar = maxcharint;
      str[0] = maxchar;
  } 
  </code></pre>
  <p>When I enter ab as input, it successfully gives back ba as output for while loop.</p>
  <p>However, I don't see a difference in my while and for loop since both transverses all the characters in the input string starting from str[0]. Why is there an output difference? Thank you.</p>