title: alphabetical sort of letters in a string C
body: <p>I am new to C programming and trying to write a program to sort a string of letters input by a user in to alphabetical order. So far I have the following code. Can anyone help me to get it to run properly? The code compiles and runs, but does not seem to store a running count correctly. </p>
  
  <p>As far as debugging it I noticed the use of letter as a counter could be an issue, but have seen this done elsewhere and their <a href="http://www.programmingsimplified.com/c/source-code/c-program-sort-string" rel="nofollow">code</a> compiles and runs fine. Thanks in advance.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  int main(int argc, char *argv[])
  {
          char str[10], strout[10] ;
          char letter ;
          int letter_count[26] = {0} ;
          int i, j , k, l, strlength ;
  
          printf("Please enter a string and I'll sort it \n") ;
          fgets(str, sizeof(str), stdin) ;
          strlength = strlen(str) ;
  
          for(i = 0 ; i &lt; strlength ; i++)
          {
                  if((str[i] &gt;= 'A') &amp;&amp; (str[i] &lt;= 'Z'))
                  {
                          printf("Capital letter\n") ;
                          letter  = str[i] - 'A' ;
                          letter_count[letter] = letter_count[l] + 1  ;
                          printf("Letter %c has a count of %d\n",str[i], letter_count[i]) ;
                  }
                  else if((str[i] &gt;= 'a') &amp;&amp; (str[i] &lt;= 'z'))
                  {
                          printf("Lower case\n") ;
                          letter = str[i] - 'a' ;
                          letter_count[letter] = letter_count[l] + 1 ;
                          printf("Letter %c has a count of %d\n",str[i], letter_count[i]) ;
                  }
          }
  
          k = 0 ;
          for( letter = 'a' ; letter &lt;= 'z' ; letter++)
          {
                  i = letter - 'a' ;
                  for(j = 0 ; j &lt;= letter_count[i] ; j++)
                  {
                          strout[k] = letter ;
                          k++ ;
                  }
  
  
          }
          return 0 ;
  }
  </code></pre>