题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h>
#include <string.h>
int main() {
char str[1000];
gets(str);
int length = strlen(str);
char temp[length];
/* 1、先将所有的字母a-z, A-Z排序*/
int k = 0;
for (int i = 0; i < 26; i++) {
for (int j = 0; j < length; j++) {
if ( str[j] == ('a' + i) || str[j] == ('A' + i)) {
temp[k++] = str[j]; // 排好序的字符串就存放在temp中
}
}
}
/* 2、将temp按照顺序写回到str中,遇到非字母就跳过 */
k = 0;
for (int i = 0; i < length; i++) {
if (((str[i] >= 'a') && (str[i] <= 'z')) || ((str[i] >= 'A') &&
(str[i] <= 'Z'))) {
str[i] = temp[k++];
}
}
printf("%s", str);
return 0;
}
/* 排序 */