如何在C中按升序对字符串数组进行排序 [英] How to sort array of strings in ascending order in C

查看:281
本文介绍了如何在C中按升序对字符串数组进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我已经制定了与其他类似的排序程序

I have made sorting program which is similiar to other found at

https: //beginnersbook.com/2015/02/c-program-to-sort-set-of-strings-按字母顺序/

但是我制作的程序无法正常工作。
我认为两者相同,但我的程序给我浪费了输出。

but program which i made is not working. I think both are same but my program giving me waste output.

我还想知道其他程序中的计数设置为5,例如从0开始接受6个输入,但仅获得5个输入,如何?

Also i want to know in other program count is set to 5 for example and it should take 6 input starting from 0 but it is getting only 5,How?

我的程序

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

int main() {

char str[4][10],temp[10];
int i,j;
printf("Enter strings one by one : \n");
for(i=0;i<5;i++)
    scanf("%s",str[i]);

for(i=0;i<5;i++)
    for(j=i+1;j<5;j++)
        if(strcmp(str[i],str[j])>0){
            strcpy(temp,str[i]);
            strcpy(str[i],str[j]);
            strcpy(str[j],temp);
        }

printf("\nSorted List : ");
for(i=0;i<5;i++)
    printf("\n%s",str[i]);
printf("\n\n");

return 0;

}


推荐答案

不是答案,而是对您引用

#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);                                        // (1)

  puts("Enter Strings one by one: ");
  for(i=0;i<=count;i++)                                      // (2)
    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:");                        // (3)
  for(i=0;i<=count;i++)
    puts(str[i]);

  return 0;
}

批评:

(1) scanf(%d,& count); 将数字读入count,然后返回。它不占用换行符(!)

(1) scanf("%d",&count); reads a number into count, and returns after that. It does not consume the line break(!)

(2),此循环不打印任何内容,只读取内容。但是,如果您

(2) this loop does not print anything, just reads. However if you put

  for(i=0;i<=count;i++){
    printf("%d:",i);
    gets(str[i]);
  }

代替,您会突然发现它要求输入名称0。 .5,只是自动跳过0。那是换行符被消耗的地方,它读取一个空字符串。您也可以显示它,而不是将 5 放入初始问题中,而不是放入 5 anmoloo7

in its place, you will suddenly see that it asks for names 0...5, just skips the 0 automatically. That is where the line break is consumed, it reads an empty string. You can also make it appear, if instead of putting 5 into the initial question, you put 5 anmoloo7.

(3),名称显示在标题排序字符串的顺序下方。但是该printf中没有换行符。问题是空字符串比任何其他字符串都小,因此它到达列表的最前面,并首先打印在该列表中。如果您在初始数字后添加名称的技巧,输出将看起来不同,将有6个名称,其中一个直接附加在标题后。

(3) in the printout the names appear below the title Order of Sorted Strings. But there is no linebreak in that printf. The thing is that the empty string is "smaller" than any other string, so it gets to the front of the list, and that is printed there first. If you do the 'trick' of appending a name after the initial number, the output will look different, there will be 6 names, and one of them appended directly to the title.

另外,您可能还可以从编译器中得到一些东西: gets 是一个致命的函数,忘记它的存在并使用 fgets 和标准答案一样,出现在其他答案中。

Plus there is the thing what you probably get from your compiler too: gets is a deadly function, forget its existence and use fgets with stdin as appears in other answers.

这篇关于如何在C中按升序对字符串数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆