题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h> #include <stdlib.h> #include <string.h> //1.从左到右遍历取出最小的字母(大写或小写) // 1.1若当前是字母把接着把最小的字母放输出, // 从左到右遍历取出当前最小的字母(大写或小写) // 有则放输出,没则找下一个字母 // 1.2若当前不是字母,则直接输出 void insert_char(char * arr, int arr_len, char insert_c, int insert_index) { int i = 0; int len = arr_len - insert_index; for (i = 0; i < len; i++) { arr[arr_len-i] = arr[arr_len-i-1]; } arr[insert_index] = insert_c; } int main() { char str[1002] = {0}; fgets(str, sizeof(str), stdin); str[strcspn(str, "\n")] = 0; str[strcspn(str, "\r")] = 0; int out_len = 0; char str_out[1001] = {0}; memset(str_out, 0, sizeof(str_out)); int len = strlen(str); //查找是否是字母 //是字母则输出,然后完后查找相同的字母并输出 char j = 0; int size = 'Z' - 'A'; for (j = 0; j <= size; j++) { int i = 0; for (i = 0; i < len; i++) { if (str[i] == 'A'+j || str[i] == 'a'+j) { str_out[out_len++] = str[i]; } } } //其他字符,则进行插入 int i = 0; for (i = 0; i < len; i++) { if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) { } else { insert_char(str_out, out_len++, str[i], i); } } printf("%s\n", str_out); return 0; }