C语言实现字符串字母排序输出
#include <stdio.h> #include <string.h>
int main() { const char* str1 = 'hello'; const char* str2 = 'world'; const char* str3 = 'apple';
const char* strings[3] = {str1, str2, str3};
int i, j;
char* temp;
// 使用冒泡排序按字母顺序排序字符串
for (i = 0; i < 2; i++) {
for (j = 0; j < 2 - i; j++) {
if (strcmp(strings[j], strings[j+1]) > 0) {
temp = strings[j];
strings[j] = strings[j+1];
strings[j+1] = temp;
}
}
}
// 输出排序后的字符串
for (i = 0; i < 3; i++) {
printf('%s\n', strings[i]);
}
return 0;
}
该程序使用冒泡排序算法对字符串进行排序,然后按顺序输出排序后的字符串。输出结果为:
apple
hello
world
原文地址: http://www.cveoy.top/t/topic/f4lO 著作权归作者所有。请勿转载和采集!