title: Name sorting in C
body: <p>Bellow a program I've made to sort names like a mobile phone does i.e sort the list with the first letter alphabetically, then with in that sorted list again sort the list with the second letters alphabetically and so on. If the two names have same letters with a or more than one extra letter added in one name then that name will appear below the other name. But I couldn't find whats wrong in this code. Please help. I've tried to make it as much as readable as possible. </p>
  
  <pre><code>#include&lt;stdio.h&gt;
      #include &lt;conio.h&gt;
      #include &lt;string.h&gt;
      #include &lt;stdlib.h&gt;
  
      int check_counter=0;
      int col_no=0;
  
      int in_check(int *check, int element_no, int total_element)
      {
           int i;
  
           for(i=0; i&lt;total_element; i++)
           {
                if(check[i]==element_no)
                return 1;
           }
  
           return 0;
      }
  
  
      int bubble_sort_chars(char **arr, int len_of_arr, int *check)
      {
           int main_counter,sub_counter1,sub_counter2;
           char char1, char2;
           char *temp;
  
           for(main_counter=len_of_arr-1,check_counter=0; main_counter&gt;0; main_counter--)//Decreasing main counter untill it indicates the position of the second element
           {
                for(sub_counter1=0,sub_counter2=1; sub_counter1&lt;main_counter; sub_counter1++,sub_counter2++)//Increasing the sub counter for a specific value of main counter untill it indicates the immidiate previous value of main counter
                {
                    //Check if the sub_counter 1 and 2 belongs to check[] or it's a space or not
                    if( in_check(check,sub_counter1,check_counter+1)==1 || *(*(arr+sub_counter1)+col_no)==' ' )
                    sub_counter1++;
                    else if( in_check(check,sub_counter2,check_counter+1)==1 || *(*(arr+sub_counter2)+col_no)==' ' )
                    sub_counter2++;
  
                    //If the sub_counter element is null then putting the position the the string in the check()
                    if(*(*(arr+sub_counter2)+col_no)==0)
                     {
                          check[check_counter]=sub_counter2;
                          check_counter++;
                          sub_counter2++;
                          continue;
                     }
  
                     char1 = *(*(arr+sub_counter1)+col_no); char2= *(*(arr+sub_counter2)+col_no);
                     //Making the first character element of each string to upper case if it's lower case
                     if( char1&gt;=97 &amp;&amp; char1&lt;=122 ) char1 -= 32;
                     if( char2&gt;=97 &amp;&amp; char2&lt;=122 ) char2 -= 32;
  
                     if(char1&gt;char2)
                     {
                          temp = *(arr+sub_counter2); 
                          *(arr+sub_counter2) = *(arr+sub_counter1);
                          *(arr+sub_counter1) = temp; 
                     }
                }
           }
  
           return check_counter;
      }
  
  
      void alphabet_sort(char **arr, int len_of_arr)
      {
           int main_counter,start_pos,ch;
           char char1,char2;
           int check[len_of_arr];
  
           ch=bubble_sort_chars(arr,len_of_arr,check);
           if( ch == len_of_arr-1 || ch == len_of_arr )
           return;
  
           for(main_counter=0; main_counter&lt;len_of_arr-1; main_counter++)
           {
                start_pos=main_counter;
                char1 = *(*(arr+main_counter)+col_no);
                if( char1&gt;=97 &amp;&amp; char1&lt;=122 ) char2 -= 32;//Making the first character element of the string to upper case if it's lower case and putting it in char2
  
                while(*(*(arr+main_counter)+col_no)==char1 || *(*(arr+main_counter)+col_no)==char2)//Counting where the alphabet ends (char1 or char2) in the sorted list of first element characters
                main_counter++;
                main_counter--;
  
                col_no++;
                alphabet_sort(arr+start_pos, main_counter-start_pos );
           }
  
           return;
      }
  
      int main()
      {
           char *name[]=
           {
                "A",
                "AB",
                "Al",
                "ABc",
                "abk",
                "Zap",
                "abce",
                "Abv",
                "abcp",
                "zop",
                "zzz",
                "P",
                "Zap",
                "Abcd",
                "Zoo",
                "A",
                "c"
           };
  
  
           alphabet_sort(name,17);
  
           int i;
           for(i=0; i&lt;17; i++)
           {
                printf("%s\n", name[i]);
           }
  
           getch();
      }
  </code></pre>