使用指针的指针对字符串排序

此页面是否是列表页或首页?未找到合适正文内容。

使用指针的指针对字符串排序

标签:printprintfpreimage数值比较colorsort地址

1 /* Note:Your choice is C IDE */
2 #include \”stdio.h\”
3 #include \”string.h\”
4 /*使用指针的指针对字符串排序*/
5 /*排序是按照汉字的首字母进行*/
6 /*
7 *整体思路:1、输出排序前的数组元素
8
9 * 2、输出排序后的数组元素
10
11 */
12 //自定义函数sort(),实现对字符串的排序
13 sort(char*strings[],int n)//参数1:字符型指针数组。参数2:整型变量
14 {
15 char *temp; //声明字符型指针变量
16 int i,j; //声明整型变量
17 for(i=0;i<n;i++) //之所以要两个循环,因为每个数都要和其他比较
18 {
19 for(j=i+1;j<n;j++)
20 {
21 /*
22 *strcmp如何实现两个字符的比较??
23 ① str1小于str2,返回负值或者-1(VC返回-1);
24 ② str1等于str2,返回0;
25 ③ str1大于str2,返回正值或者1(VC返回1);
26 */
27 if(strcmp(strings[i],strings[j])>0)//比较两个字符
28 {
29 temp=strings[i];//交换字符位置
30 strings[i]=strings[j];
31 strings[j]=temp;
32 }
33 }

作者: 电脑大师

为您推荐

返回顶部