1. 程式人生 > >輸入6個字串,並對它們按從小到大的順序排序後輸出。

輸入6個字串,並對它們按從小到大的順序排序後輸出。

    printf("請輸入六個字串\n");
    char str[6][10] = {0};
    for (int i = 0; i < 6; i++) {
        scanf("%s",str[i]);
    }
    for (int i = 0; i < 6 - 1; i++) {
        for (int j = 0; j < 6 - 1 -i; j++) {
            if (strcmp(str[j], str[j + 1]) > 0) {
                char strtemp[10] = {0};
                strcpy(strtemp, str[j]);
                strcpy(str[j], str[j + 1]);
                strcpy(str[j + 1], strtemp);
            }
        }
    }
    printf("排好序後字串順序為:\n");
    for (int i = 0; i < 6; i++) {
        printf("%s\n",str[i]);
    }