在主函数中输入的一个全为小写字母的字符串,编写一个子函数,能够对这个字符串进行从a-z的顺序排序。
#include <stdio.h>
#include <string.h>
void sortString(char str[], int len) {
int i, j, minIndex;
char temp;
for (i = 0; i < len - 1; i++) {
minIndex = i;
for (j = i + 1; j < len; j++) {
if (str[j] < str[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
temp = str[i];
str[i] = str[minIndex];
str[minIndex] = temp;
}
}
}
int main() {
char str[100];
printf("Enter a lowercase string: ");
scanf("%s", str);
int len = strlen(str);
sortString(str, len);
printf("The sorted string is: %s\n", str);
return 0;
}