36: String Sort sortstring

Source: Internet
Author: User

Title Description: 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: a nested loop to sort the letters in the string, put all the sorted letters in the temp[], the last output, traversing the original character array, if one is a letter, then go to the temp in the corresponding position has been sorted alphabetically output, non-letter, the output of the original position character.

1 /*Title Description: Write a program that sorts the characters in the input string according to the following rules. 2 Rule 1: English letters are arranged from A to Z, and are not case sensitive. 3 For example, input: type output: Epty4 Rule 2: When the case of the same English letter exists, it is arranged in the order of entry. 5 For example, input: BabA output: AABb6 Rule 3: Other characters of non-English letters remain in their original position. 7 For example, input: By?e output: Be?y8 Examples:9 Input:Ten A Famous Saying:much Ado About Nothing (2012/8). One Output: A A aaaabc Dfgghh:iimm nNn oooos sttuuuy (2012/8). - */ - ImportJava.util.Scanner; the  -  Public classMain { -  -      Public Static voidMain (string[] args) { +         //TODO auto-generated Method Stub -Scanner in =NewScanner (system.in); +          while(In.hasnext ()) A         { atString input =in.nextline (); -             intLength =input.length (); -             Char[] Array =Input.tochararray (); -             //all alphabetical sort in input -             Char[] temp =New Char[length]; -             intK = 0; in              for(inti = 0; i<26; i++) -             { to                  for(intj = 0; j<length; J + +) +                 { -                     if(Array[j]-' a ' = = I | | array[j]-' a ' = =i) the                     { *temp[k++] =Array[j]; $                     }Panax Notoginseng                 } -             } the             //notice!! +K = 0; A              for(inti = 0; i<length; i++) the             { +                 if(Array[i] >= ' A ' && array[i] <= ' z ' | | array[i] >= ' A ' && array[i] <= ' z ') -                 { $Array[i] = temp[k++]; $                     //Array[i] = temp[i]; -                 } -             } the System.out.println (string.valueof (array)); -         }Wuyi     } the  -}

36: String Sort sortstring

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.