c - 按字母顺序对单词数组进行排序

标签 c arrays sorting

我正在自学 C 语言,为即将到来的学期做准备,并且想知道到目前为止我的代码做错了什么。

如果事情看起来很奇怪,那是因为这是我创建的一个更大的排序函数的一部分,目的是了解如何对数字、字母、数组等进行排序!目前,我在 C 语言中的字符串操作方面基本上遇到了一些麻烦。

而且,目前我对 C 的了解还很有限!

我的主要内容包括:

#include <stdio.h>
#include <stdio.h>                                                           
#include <stdlib.h>                                                          
int numbers[10];                                                             
int size;                                                                    
int main(void){                                                              
    setvbuf(stdout,NULL,_IONBF,0); //This is magical code that allows me to input.

    int wordNumber;                                                          
    int lengthOfWord = 50;                                                   

    printf("How many words do you want to enter: ");                         
    scanf("%i", &wordNumber);                                                
    printf("%i\n",wordNumber);                                               

    char words[wordNumber][lengthOfWord];                                    
    printf("Enter %i words:",wordNumber);                                    
    int i;                                                                   

      for(i=0;i<wordNumber+1;i++){ //+1 is because my words[0] is blank.     
            fgets(&words[i], 50, stdin);                                     
      }                                                                      

      for(i=1;i<wordNumber+1;i++){ // Same as the above comment!             
            printf("%s", words[i]); //prints my words out!                   
      }                                                                      
    alphabetize(words,wordNumber); //I want to sort these arrays with this function.
}

我试图构建的排序“方法”如下!这个函数有严重缺陷,但我想我应该保留所有这些,以便向您展示我在写这篇文章时的想法。

void alphabetize(char a[][],int size){ // This wont fly.
            size = size+1;
            int wordNumber;
            int lengthOfWord;
            char sortedWords[wordNumber][lengthOfWord]; //In effort for the for loop
            int i;
            int j;

            for(i=1;i<size;i++){ //My effort to copy over this array for manipulation
                    for(j=1;j<size;j++){
                            sortedWords[i][j] = a[i][j];
                    }
            }
            //This should be kinda what I want when ordering words alphabetically, right?
            for(i=1;i<size;i++){
                    for(j=2;j<size;j++){
                    if(strcmp(sortedWords[i],sortedWords[j]) > 0){
                            char* temp = sortedWords[i];
                            sortedWords[i] = sortedWords[j];
                            sortedWords[j] = temp;
                    }
            }
            }
            for(i=1;i<size;i++){
                    printf("%s, ",sortedWords[i]);
            }
    }

我想我还有另一个问题...... 当我使用 fgets() 时,它正在做这件事,我在数组的第一个位置得到一个空字。我最近遇到了其他问题,试图以某种方式专门扫描我的输入单词变量来 scanf() char[] ,这“神奇地”消除了字符之前的第一个空空格。一个例子是使用 scanf() 写入“Hello”并获取“Hello”或“”“Hello”...

感谢对此的任何想法,我整个夏天都在学习,所以不需要匆忙回答!另外,感谢 Stack Overflow 作为一个整体在过去提供的帮助。这可能是我的第一篇文章,但过去几年我一直是常客,它是提供有用建议/提示的最佳场所之一。

最佳答案

您会喜欢这个 - 它是 QSort 的派生,适合您的情况。如果不在这里或那里进行修饰,它可能不太适合您(首先测试!):

void qsort (Strings[], NumberOfItems)
{
   char Temp[51];    // Assumes your max string length of 50
   int I1 = 0;       // Primary index
   int I2 = 0;       // Index + 1
   int NumberOfItems_1 = 0;
   bool Swapped = false;
   do // Outer loop
   {
      Swapped = false;
      // Decrement our limit
      NumberOfItems--;

      // Save time not performing this subtraction many times
      NumberOfItems_1 = NumberOfItems - 1;

      // Repeatedly scan the list
      for (I1 = 0; I1 < NumberOfItems_1; I1++)
      {
         // Save time not performing this addition many times
         // I1 points to the current string
         // This points to the next string
         I2 = I1 + 1;

         // If the current string is greater than the next string in the list,
         // swap the two strings
         if (strcmp(Strings[I1], Strings[I2]) > 0)
         {
            strcpy (Temp, Strings[I1]);
            strcpy (Strings[I1], Strings[I2]);
            strcpy (Strings[I2], Temp);
            Swapped = true;
         }
      }
   }
   while (Swapped);     // Break out when we've got nothing left to swap
}

关于c - 按字母顺序对单词数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23925021/

相关文章:

c# - 如何捕获在 Windows Server 2003 中取消的注销

c - 字符串是否包含列表中的单词

c - 这种结构的大小如何出乎意料?

c++ - 为什么可以将一个数组赋值给一个char指针?

javascript - React 无法读取属性值

javascript - ReactJS:如何根据 Prop 的值(value)对对象数组进行排序?

c - 在读写器问题中什么时候唤醒作者?释放互斥体之前还是之后?

javascript - AJAX 请求从包含对象的数组中提取信息

当数据类型为 bigint(8) 但作为 bigint(20) 失败时,mysql 排序功能正常

MySQL 特殊排序