[华为]字符串排序

字符串排序

编写一个程序,将输入字符串中的字符按如下规则排序。

规则1:英文字母从A到Z排列,不区分大小写。

      如,输入:Type 输出:epTy

规则2:同一个英文字母的大小写同时存在时,按照输入顺序排列。

    如,输入:BabA 输出:aABb

规则3:非英文字母的其它字符保持原来的位置。

    如,输入:By?e 输出:Be?y

样例:

    输入:

   A Famous Saying: Much Ado About Nothing(2012/8).

    输出:

   A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).

 1 #include<vector>
 2 #include<iostream>
 3 #include<string> 
 4 using namespace std;
 5 int main()
 6 {    
 7     string s;    
 8     vector<char> tempChar;    
 9     while(getline(cin,s))    
10     {        
11         tempChar.clear();        
12         int len = s.size();       
13         for(int j=0; j<26; j++)        
14         {            
15             for(int i=0; i<len; i++)           
16             {                
17                 if(s[i]-'a'==j||s[i]-'A'==j)                
18                 {                    
19                     tempChar.push_back(s[i]);               
20                 }            
21             }        
22         }        
23         
24         for(int i=0,k=0;(i<len)&&k<tempChar.size();i++)        
25         {            
26             if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))               
27                 s[i]=tempChar[k++];        
28         }        
29         
30         cout<<s<<endl;    
31     }    
32     return 0;
33 }
原文地址:https://www.cnblogs.com/hellochennan/p/6668866.html