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

/* Demo 03：数组与字符串 */

void bubbleSort(int arr[], int n)
{
    for (int i = 0; i < n - 1; i++)
        for (int j = 0; j < n - 1 - i; j++)
            if (arr[j] > arr[j + 1])
            {
                int t = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = t;
            }
}

int main()
{
    // 一维数组 + 冒泡排序
    int scores[] = {66, 90, 78, 85, 92};
    int n = sizeof(scores) / sizeof(scores[0]);
    bubbleSort(scores, n);
    printf("排序后：");
    for (int i = 0; i < n; i++)
        printf("%d ", scores[i]);
    printf("\n");

    // 二维数组
    int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int sum = 0;
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 3; j++)
            sum += matrix[i][j];
    printf("矩阵元素和 = %d\n", sum);   // 21

    // 字符串
    char str1[20] = "hello";
    char str2[10] = "world";
    printf("strlen(str1) = %zu, sizeof(str1) = %zu\n", strlen(str1), sizeof(str1));
    printf("strcmp(str1, str2) = %d（<0 说明 str1 字典序更小）\n", strcmp(str1, str2));

    strcat(str1, ", ");
    strcat(str1, str2);
    printf("拼接后：%s\n", str1);   // hello, world

    char *sub = strstr(str1, "world");
    if (sub != NULL)
        printf("找到子串，从 %s 开始\n", sub);

    return 0;
}
