题解 | #字符串排序#提供一个冒泡排序的解法吧,C
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h> #include <string.h> #define SIZE 1005 int JudgeSequence(char a, char b) { /* 将大写字母转为小写字母 */ if(a - 'a' < 0) { a += 32; } if(b - 'a' < 0) { b += 32; } if(a > b) { return 1; } return 0; } int JudgeIsNotLetter(char c) { if(c < 'A' || (c > 'Z' && c < 'a') || c > 'z') { return 1; } return 0; } int main() { char str[SIZE]; fgets(str, SIZE, stdin); int len = strlen(str); if(str[len - 1] == '\n') { str[--len] = '\0'; } for(int i = 0; i < len; i++) { for(int j = 0; j < len - i - 1;) { if(JudgeIsNotLetter(str[j])) { //找到第一个字母下标; j++; continue; } for(int k = j + 1; k < len; k++) { //找到下一个字母的下标; if(JudgeIsNotLetter(str[k])) { if(k == len - 1) { //如果后面都没有字母了,就要刷新j的值,让其跳出上层循环; j = k; } continue; } if(JudgeSequence(str[j], str[k])) { char temp = str[j]; str[j] = str[k]; str[k] = temp; } j = k; } } } printf("%s\n", str); return 0; }
#冒泡排序divdivforiinrangen1divdivforjinrangeni1divdivifbjgtbj1divdivbjbj1bj1bjdivdivajaj1aj1ajdivdiv#