P216 12.写一个函数,用冒泡法对输入的10个字符按由小到大顺序排列
#include#includeint main() {void sort(char str[]);int i = 0, flag = 1;char string[20];for (flag = 1; flag == 1;) {printf("请输入10个字符:");gets(string);if (strlen(string) > 10) {printf("输入的字符过多,请重新输入");}else {flag = 0;}}sort(string);//输出for (i = 0; i < 10; i++) {printf("%c", string[i]);}return 0;}void sort(char str[]) {int i = 0, j = 0;char t;for (i = 1; i < 10; i++) {for (j = 0; (j str[j + 1]) {t = str[j];str[j] = str[j + 1];str[j + 1] = t;}}}}
运行结果: