字符串排序(C语言 + qsort实现 + 详细注释)

#include<stdio.h>
#include<stdlib.h>

int cmp(char *s1, char *s2);
int main(){
    int i;
    char s[5][85];
    for(i = 0; i < 5; i++)
        scanf("%s", s[i]);               //读入5个字符串
    qsort(s, 5, sizeof(s) / 5, cmp);     //sizeof(s) / 5 为每个一维数组的大小
    printf("After sorted:\n");
    for(i = 0; i < 5; i++)
        printf("%s\n", s[i]);


    return 0;
}


int cmp(char *s1, char *s2){           //自写比较函数
    return strcmp(s1, s2);
}
 

C语言qsort库函数使用说明: https://blog.csdn.net/linaijunix/article/details/50358518

发布了30 篇原创文章 · 获赞 10 · 访问量 414

猜你喜欢

转载自blog.csdn.net/qq_45472866/article/details/104050490