c - C 中的名称排序

标签 c string sorting

下面是我制作的一个像手机一样对名称进行排序的程序,即按第一个字母按字母顺序对列表进行排序,然后在该排序列表中再次按第二个字母按字母顺序对列表进行排序,依此类推。如果两个名字具有相同的字母,并且一个名字中添加了一个或多个额外的字母,那么该名字将出现在另一个名字的下方。但我找不到这段代码有什么问题。请帮忙。我已尽力使其尽可能具有可读性。

#include<stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <stdlib.h>

    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<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>0; main_counter--)//Decreasing main counter untill it indicates the position of the second element
         {
              for(sub_counter1=0,sub_counter2=1; sub_counter1<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>=97 && char1<=122 ) char1 -= 32;
                   if( char2>=97 && char2<=122 ) char2 -= 32;

                   if(char1>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<len_of_arr-1; main_counter++)
         {
              start_pos=main_counter;
              char1 = *(*(arr+main_counter)+col_no);
              if( char1>=97 && char1<=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<17; i++)
         {
              printf("%s\n", name[i]);
         }

         getch();
    }

最佳答案

关于:

for( main_counter=0; main_counter<len_of_arr-1; main_counter++ )
{
    start_pos=main_counter;
    char1 = *(*(arr+main_counter)+col_no);
    if( char1>=97 && char1<=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 );
}

main_counter-start_pos 的结果是 0 或 -1

关于c - C 中的名称排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118941/

相关文章:

c - 3d 加速度计计算方向

创建 IWebBrowser2 控件

c++ - 如果字符串在数组中,这样做吗?

c - 如何在C中按升序对字符串数组进行排序

sorting - SubDataFrame 的自定义排序

c - 模拟 printf 堆栈弹出

c - malloc() 调用后双重释放或损坏(快速停止)

C 打印两个字符串的第二个字符串值

java - 具有多个分隔符的 Split()(不起作用)

javascript - 在 Javascript 中对 2D 数组/网格/项目表进行排序