浙大版《C语言程序设计(第3版)》题目集 习题8-7 字符串排序 (20分)

在这里插入图片描述

#include <stdio.h>
#include <string.h>
#define MAXN 80
int main()
{
    char str[5][MAXN], t[MAXN];
    int i, j;
    for (i = 0; i < 5; i++)
        scanf("%s", &str[i]);
    for (i = 1; i < 5; i++)
        for (j = 0; j < 5 - i; j++)
            if (strcmp(str[j], str[j + 1]) > 0)
            {
                strcpy(t, str[j]);
                strcpy(str[j], str[j + 1]);
                strcpy(str[j + 1], t);
            }
    printf("After sorted:\n");
    for (i = 0; i < 5; i++)
        printf("%s\n", str[i]);
    return 0;
}
发布了252 篇原创文章 · 获赞 117 · 访问量 8549

猜你喜欢

转载自blog.csdn.net/qq_44458489/article/details/105321482