题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
//重复循环读取输入的字符串,从A到Z,依次找出来保存到数组out中,规则1就实现了;如果是符号则直接保存到对应位置,规则3实现了;这种方式本身就是按照输入的前后顺序保存的,所以规则2也实现了; #include<stdio.h> int main(){ char in[1024]; char out[1024]; char p='A'; // 从A到Z int j=0; // out数组的当前下标 while(gets(in)) { memset(out,0,sizeof(out)); j=0; p='A'; while(j < strlen(in)) { for(int i= 0; i<strlen(in); i++) { if((in[i]>='A' && in[i]<='Z') || (in[i]>='a' && in[i]<='z')) { if(out[j] != NULL) { // 非NULL表示被当前位置已经填的是符号了 j++; i--; continue; } if(in[i] == p || in[i] == p+32) { // 匹配到大小写 out[j++] = in[i]; } } else { // 符号则直接存放到out对应位置 out[i] = in[i]; } } p++; } printf("%s\n", out); } }