#include <stdio.h>

int main() { char* strings[] = {'Data structure', 'Computer design', 'C Progrom'}; int numStrings = sizeof(strings) / sizeof(strings[0]);

// 使用冒泡排序对字符串进行排序
for (int i = 0; i < numStrings - 1; i++) {
    for (int j = 0; j < numStrings - i - 1; j++) {
        if (strcmp(strings[j], strings[j + 1]) > 0) {
            // 交换字符串
            char* temp = strings[j];
            strings[j] = strings[j + 1];
            strings[j + 1] = temp;
        }
    }
}

// 按顺序打印字符串
printf('排序后的字符串:\n');
for (int i = 0; i < numStrings; i++) {
    printf('%s\n', strings[i]);
}

return 0;

}

C语言指针数组实现字符串排序 - 按字符顺序输出

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

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