加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

逻辑挑战:在C中按字母顺序排序数组

发布时间:2020-12-16 10:40:47 所属栏目:百科 来源:网络整理
导读:我是编程的新手,目前正在学习C.我已经在这个问题上工作了一个星期了,我似乎无法理解逻辑.这直接来自我正在使用的书: Build a program that uses an array of strings to store the following names: “Florida” “Oregon” “Califoria” “Georgia” Usin
我是编程的新手,目前正在学习C.我已经在这个问题上工作了一个星期了,我似乎无法理解逻辑.这直接来自我正在使用的书:

Build a program that uses an array of strings to store the following names:

  • “Florida”
  • “Oregon”
  • “Califoria”
  • “Georgia”

Using the preceding array of strings,write your own sort() function to display each state’s name in alphabetical order using the strcmp() function.

所以,让我说我有:

char *statesArray[4] = {"Florida","Oregon","California","Georgia"};

我应该嵌套for循环,比如strcmp(string [x],string [y])…?我已经入侵并砍掉了.我只是无法绕过解决这个问题所需的算法,甚至有效.帮助很多赞赏!

解决方法

是的,您可以使用嵌套的for循环进行排序.在了解strcmp()如何工作之后,它应该是相当直接的:

strcmp(char *string1,char *string2)

  • if Return value is < 0 then it indicates string1 is less than string2

  • if Return value is > 0 then it indicates string2 is less than string1

  • if Return value is = 0 then it indicates string1 is equal to string2

然后,您可以从此处选择任何一种排序方法

This site有大量正在执行的各种类型的图形示例,并包括给定算法的伪代码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读