22.md 1.4 KB
Newer Older
W
wizardforcel 已提交
1
# C 程序:按字母顺序对字符串集进行排序
W
wizardforcel 已提交
2 3 4 5 6

> 原文: [https://beginnersbook.com/2015/02/c-program-to-sort-set-of-strings-in-alphabetical-order/](https://beginnersbook.com/2015/02/c-program-to-sort-set-of-strings-in-alphabetical-order/)

在以下程序中,将要求用户输入一组字符串,程序将按字母顺序升序排序并显示它们。

W
wizardforcel 已提交
7
#### C 程序 - 对一组字符串按字母顺序升序排序
W
wizardforcel 已提交
8

W
wizardforcel 已提交
9
```c
W
wizardforcel 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* This program would sort the input strings in
 * an ascending order and would display the same
 */
#include<stdio.h>
#include<string.h>
int main(){
   int i,j,count;
   char str[25][25],temp[25];
   puts("How many strings u are going to enter?: ");
   scanf("%d",&count);

   puts("Enter Strings one by one: ");
   for(i=0;i<=count;i++)
      gets(str[i]);
   for(i=0;i<=count;i++)
      for(j=i+1;j<=count;j++){
         if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
         }
      }
   printf("Order of Sorted Strings:");
   for(i=0;i<=count;i++)
      puts(str[i]);

   return 0;
}
```

**输出:**
![sorted_strings_ascending](img/295926e972952915c59b1586cfc045bc.jpg)

正如你在上面的输出屏幕截图中观察到的那样,我们输入了 5 个字符串,程序然后按升序对它们进行排序。我们得到一组有序的字符串作为输出。