Huawei Training Questions: Intermediate--string sorting (Find)

Source: Internet
Author: User

Write a program that sorts the characters in the input string according to the following rules.

Rule 1: English letters are arranged from A to Z, and are not case sensitive.

For example, input: type output: Epty

Rule 2: When the case of the same English letter exists, it is arranged in the order of entry.

For example, input: BabA output: aABb

Rule 3: Other characters of non-English letters remain in their original position.

For example, input: By?e output: Be?y

Examples:

Input:

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

Output:

A aaaabc Dfgghh:iimm nNn oooos sttuuuy (2012/8).

Idea: My idea is to first remove the letters from the string, put them in another string, and sort them in this string.

Then the ordered word assigned value to the previous string, overwriting the corresponding letters.

This one because the letters are not case-sensitive, and the same to press before the order, I began to bubble do, the results found wrong

Then I think of the method of searching and inserting, traversing 26 times, from A to Z, then finding the trailing end into a string.

The following is the AC code

1#include <iostream>2#include <string>3 using namespacestd;4 5 voidSorted2 (string&str)6 {7     stringstr1;8     intlen=str.size ();9      for(intI=0;i< -; i++)Ten     { One          for(intj=0; j<len;j++) A             if(str[j]== ('A'+i) | | str[j]== ('a'+i)) - Str1.push_back (Str[j]); -     } theStr=str1; - } - /* - void sorted (string &str) + { - if (Str.empty ()) + return; A int len=str.size (); at if (str.size () ==1) - return; - for (int i=0;i<len-1;i++) -     { - for (int j=i+1;j<len;j++) -         { in Char A, b; - if (str[i]>= ' a ' &&str[i]<= ' z ') to a=str[i]-32; + Else - A=str[i]; the if (str[j]>= ' a ' &&str[i]<= ' z ') * b=str[j]-32; $ ElsePanax Notoginseng B=str[j]; -  the if (a>b) +             { A char temp; the Temp=str[i]; + Str[i]=str[j]; - str[j]=temp; $             } $         } -     } - return; the } - */Wuyi voidChangeword (string&str) the { -     if(Str.empty () | | Str.size () = =1) Wu         return; -     stringstr1; About      for(string:: Iterator Iter=str.begin (); Iter!=str.end (); iter++) $     { -         if((*iter) >='A'&& (*iter) <='Z')|| ((*iter) >='a'&& (*iter) <='Z')) -Str1.push_back (*iter); -     } A Sorted2 (str1); +  the     intj=0; -      for(string:: Iterator I=str.begin (); I!=str.end (); i++) $         if((*i>='A'&&*i<='Z')|| (*i>='a'&&*i<='Z')) the         { the*i=Str1[j]; theJ + +; the         } -     return; in } the  the intMain () About { the     stringStr//= "A Famous saying:much Ado About Nothing"; the getline (CIN,STR); the Changeword (str); +cout<<str<<Endl; -  the     //System ("pause");Bayi}

Huawei Training Questions: Intermediate--string sorting (Find)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.