通过对指针数组进行排序来对字符串进行排序 [英] Sorting strings by sorting an array of pointers

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

问题描述

(1).定义指针数组,如下所示:

(1).defines a pointer array as follows:

char *name[] ={"January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November",
"December"};



其中name是指向字符串的指针数组.

(2).通过在名称数组中排列值,按字母顺序对月份名称进行排序,以便
name [0]指向"April",name [1]指向"August",依此类推.

(3).按字母顺序打印月份名称,每行一个名称.

对于步骤(2),您可以使用任何排序算法(例如选择排序,气泡排序或快速排序
算法)对名称数组进行排序.您可以使用标准的C库函数strcmp()来
比较两个字符串.该函数在系统头文件string.h中声明,如下所示:
int strcmp(const char * s1,const char * s2);

它比较两个字符串s1和s2.如果
,则返回小于,等于或大于零的整数. 发现s1分别小于,匹配或大于s2.



我在编程方面真的很糟糕,因此不胜感激..非常感谢:)



where name is an array of pointers pointing to strings.

(2).sorts the month names in alphabetic order by arranging the values in the name array so that
name[0] points to "April ", name[1] points to "August ", and so on.

(3).prints the month names in alphabetic order, one name per line.

For step (2), you can use any sorting algorithm (such as the selection sort, bubble sort or quick sort
algorithm) to sort the name array. You can use the standard C library function strcmp() to
compare two strings. The function is declared in the system header file string.h as follows:
int strcmp(const char *s1, const char *s2);

It compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if
s1 is found, respectively, to be less than, to match, or be greater than s2.



i am really bad when it comes to programming so any help would be appreciated..thanks a lot:)

推荐答案

这是一项家庭作业.请注意,我们不会为您编写代码.

你挂哪一部分了?
This is a homework assignment. Just to be clear, we''re not going to write your code for you.

What part of this are you hung up on?


bowww写道:

我真的很糟糕涉及到编程,因此将不胜感激..非常感谢

i am really bad when it comes to programming so any help would be appreciated..thanks a lot


那职业转变呢?
:)


What about a career change?
:)


由于可以使用 any 排序算法,因此可以比较头部中的字符串并为结果提供编码输出.问题已解决.
Since you can use any sorting algorithm, compare the strings in your head and provide coded output for the results. Problem solved.


这篇关于通过对指针数组进行排序来对字符串进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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