题解 | #字符串排序# wotcl
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h> #include <string.h> #include <ctype.h> int main() { int i,count=0; char str[2000],str_n[2000],ch1,ch2,cht; int record[2000]={0}; fgets(str,2000,stdin); for(i=0;i<strlen(str)-1;i++) { if(isalpha(str[i])) { str_n[count]=str[i]; record[i]=1; count++; } } for(i=0;i<count;i++) { for(int j=0;j<count-1;j++) { ch1=toupper((int )str_n[j]); ch2=toupper((int) str_n[j+1]); if(ch1>ch2) { cht=str_n[j]; str_n[j]=str_n[j+1]; str_n[j+1]=cht; } } } count=0; for(i=0;i<strlen(str);i++) { if(record[i]) { printf("%c",str_n[count]); count++; } else printf("%c",str[i]); } return 0; }