c语⾔实现字符指针(字符串)数组的排序需求:
"ff555d", "114ddd", "114dd","aaa", "aaab", "aaa" d对它们进⾏排序
头⽂件:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
函数原型:
void printArray(char **buff,int len);
void sortBuff(char **buff[],int len);
实现⽅法:
void printArray(char **buff, int len){
int i;
for (i = 0; i < len; ++i){
printf("%s\n", buff[i]);
}字符串长度排序c语言
}
1void sortBuff(char **buff,int len){
2
3char *temp;    //零时交换变量
4
5int i, j;
6
7/*选择排序法*/
8for (i = 0; i < len; ++i){
9
10for (j = i + 1; j < len; ++j){
11
12if( strcmp(buff[i], buff[j]) > 0){ //应⽤string.h
13//    int strcmp(
14                temp = buff[i];                    //        const char *string1,
15//        const char *string2
16                buff[i] = buff[j];                //    );
17//string1 > string2 返回值⼤于0 , == 为等于0, < 为⼩于0
18                buff[j] = temp;
19
20
21
22
23            }
24
25        }
26
27    }
28
29 }
View Code
测试:
1void main(){
2
3char *buff[] = {"ff555d", "114ddd", "114dd","aaa", "aaab", "aaa"};
4
5    printf("排序前\n");
6
7    printArray(buff, sizeof(buff) / sizeof(buff[0]));
8
9    printf("排序后\n");
10
11    sortBuff(buff, sizeof(buff) / sizeof(buff[0]));
12
13    printArray(buff, sizeof(buff) / sizeof(buff[0])); 14
15    system("pause");
16 }
运⾏结果:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。