用c语言编辑代码实现以下功能设计一个程序将3个字符串常量按字母顺序输出。
#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\n%s\n%s\n", str1, str2, str3);
return 0;
}
原文地址: http://www.cveoy.top/t/topic/hDKT 著作权归作者所有。请勿转载和采集!