title: C: Alphabetical sorting of array of character arrays
body: <p>This showed up in our laboratory finals examination:
  Make a program that takes in 10 string inputs into an array.
  Then outputs the strings in alphabetical order.</p>
  
  <p>I couldn't figure it out during the examination and now I want to know how exactly is it done.</p>
  
  <p>So far this is what I've done. It doesn't work well with similar or equivalent strings, their index gets lost? Anyone can share their solution using only the stdio.h and string.h libraries?</p>
  
  <pre><code>   /*Write a program that takes 10 strings input into an array and outputs them in alphabetical order*/
  
      #include&lt;stdio.h&gt;
      #include&lt;string.h&gt;
  
      char strings[10][150];
      char ordered[10][150];
      int i,j,k;
      int ind;
  
      main()
      {
          printf("INPUT 10 STRINGS\n");
          for(i=0;i&lt;10;i++)
          {
              gets(strings[i]);
          }
  
          for(i=0;i&lt;10;i++)
          {
              ind=0;
              for(j=0;j&lt;10;j++)
              {
                  if(strings[i][0]&lt;strings[j][0])
                  {
                      ind++;
                  }
                  else if(strings[i][0]==strings[j][0])
                  {
                      k=0;
                      while((strings[i][k]==strings[j][k])&amp;&amp;strings[j][k+1]!='\0')
                      {
                          if(strlen(strings[i])&lt;strlen(strings[j]))
                          {
                              if(strings[i][k+1]=='\0')
                              {
                                  ind++;
                              }
                              else if(strings[i][k+1]&lt;strings[j][k+1])
                              {
                                  ind++;
                              }
                          }
                          else if(strlen(strings[i])&gt;strlen(strings[j]))
                          {
                              if(strings[i][k+1]&lt;strings[j][k+1])
                              {
                                  ind++;
                              }
                          }
                          k++;
                      }
                  }
              }
              strcpy(ordered[ind],strings[i]);
          }
  
  
          printf("STRINGS: \n");
          for(i=9;i&gt;-1;i--)
          {
          puts(ordered[i]);
          }
      }
  </code></pre>