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

int main() { const char* str1 = 'hello'; const char* str2 = 'world'; const char* str3 = 'abc';

const char* temp;

// 按字母顺序比较并交换字符串常量的顺序
if (strcmp(str1, str2) > 0) {
    temp = str1;
    str1 = str2;
    str2 = temp;
}
if (strcmp(str1, str3) > 0) {
    temp = str1;
    str1 = str3;
    str3 = temp;
}
if (strcmp(str2, str3) > 0) {
    temp = str2;
    str2 = str3;
    str3 = temp;
}

// 输出按字母顺序排序后的字符串常量
printf('%s

%s %s ', str1, str2, str3);

return 0;

}

C语言实现字符串排序输出 - 代码示例

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

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