题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
//字符串排序 #include <stdio.h> #include <string.h> int TypeofChar(char val) { if (val >= 65 && val <= 90) return 1; if (val >= 97 && val <= 122) return 2; return 0; } int main() { char str[1000];//="A Famous Saying: Much Ado About Nothing (2012/8)."; gets(str); //printf("%s",str); int length = strlen(str); //又是冒泡的感觉 // for(int i=1;i<length;i++) // { // int curtype=TypeofChar(str[i]); // int prevtype=TypeofChar(str[i-1]) // if(curtype=1) // } char result[length + 1]; result[length]=0;//结束符赋值 char* searchbegin = str; char findchr = 'A'; //不需要,我就直接每个字母找一遍 for (int i = 0; i < length; i++) { //首先还是先固定下来非英文字符的位置 int curtype = TypeofChar(str[i]); if (curtype == 0) { result[i] = str[i]; continue; } //同时找findchr的大小写,若均找到则返回指针 //int checknum=0; while (findchr <= 'Z' && searchbegin != NULL) { //searchbegin!=NULL 这个条件似乎不需要 char* smaletter = strchr(searchbegin, findchr + 32); char* capletter = strchr(searchbegin, findchr); // 根据指针位置判断谁在前,赋值给当前result[i] if (smaletter != NULL) { if (capletter != NULL) { //大小写均出现 if (capletter - smaletter > 0) { //小写在前 result[i] = findchr + 32; searchbegin = smaletter + 1; // checknum=1; break; } else { result[i] = findchr; searchbegin = capletter + 1; // checknum=1; break; } } else { //只出现小写 result[i] = findchr + 32; searchbegin = smaletter + 1; // checknum = 1; break; } } else if (capletter != NULL) { //只出现大写 result[i] = findchr; searchbegin = capletter + 1; // checknum = 1; break; } else { //均未搜到 findchr++; searchbegin = str; } } //if(checknum==1) } printf("%s", result); return 0; }