#include <stdio.h>
#include <string.h>

#define MAX_LEN 20
#define NUM_STR 4

int main() {
    char str[NUM_STR][MAX_LEN+1]; // 二维字符数组,存储输入的字符串
    int i, j;

    // 从键盘输入4个字符串
    for (i = 0; i < NUM_STR; i++) {
        printf("请输入字符串%d:", i+1);
        scanf("%s", str[i]);
    }

    // 冒泡排序
    char temp[MAX_LEN+1];
    for (i = 0; i < NUM_STR-1; i++) {
        for (j = 0; j < NUM_STR-1-i; j++) {
            if (strcmp(str[j], str[j+1]) > 0) {
                strcpy(temp, str[j]);
                strcpy(str[j], str[j+1]);
                strcpy(str[j+1], temp);
            }
        }
    }

    // 输出排序后的字符串
    printf("排序后的字符串:\n");
    for (i = 0; i < NUM_STR; i++) {
        printf("%s\n", str[i]);
    }

    return 0;
}
``
用C语言写编写程序从键盘输人4个字符串长度20存人二维字符数组中。然后对它们进行排序假设由小到大顺序最后输出排序后的4个字符串允许使用字符串函数。【提示】字符串比较可以用strcmp函数实现排序方法有选择法或冒泡法。

原文地址: http://www.cveoy.top/t/topic/fR2k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录