String string processing

Source: Internet
Author: User

Problem Description:

Write a program that handles a string containing only ' 0 ' ~ ' 9 ' and ' a ' ~ ' Z ' of the 36 ASCII characters. Your program should reorder the characters in the string and divide the input string into multiple segments.

The following are the requirements for the output string:

1 The characters in each paragraph should be in strict ascending order. The sorting criterion here is the ASCII code value according to the character (e.g. ' 9 ' is greater than ' 0 ', ' z ' is greater than ' a ')

2 the characters in the 2nd paragraph must be a subset of the characters in the 1th paragraph (which can be the same), the characters in the 3rd paragraph must be a subset of the characters in the 2nd paragraph, and so on, the character of paragraph I must be a subset of the characters in paragraph i-1.

3 when the input string contains illegal characters (for example, not between ' 0 ' ~ ' 9 ' and ' a ' ~ ' Z '), your program should output "Invalid input string"

Input Sample:

Aabbccdd

007799aabbccddeeff113355zz

1234.89898

Abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee

Sample output:

Abcdabcd

013579abcdefz013579abcdefz

Invalid input string

Abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

Problem Analysis:

The input string is output in the specified order, since the Java string type declaration cannot be changed, so you might consider converting the input string to a string array, then traversing the array and processing the individual characters, and finally outputting

The title requires--1 characters to be arranged in ascending order, and the numbers are preceded by letters; 2 The string fragment output and subsequent output characters are a subset of the preceding output characters.

Consider traversing the string array from beginning to end, the number of occurrences of each character is recorded to the corresponding position of the character statistic array, and then traverse the character statistic array, because the character statistic array stores the string array by word Fu Shen order, so we use the String API append (char ch) function, The actual characters of each count position are added to StringBuffer, each adding a character, minus 1 for the corresponding position element of the string statistic array, the loop operation string counts the array until all the position character repeats are 0, and the end operation returns the StringBuffer content.

1  Packageedu.fjnu.string.demo.test;2 3 ImportJava.util.Scanner;4 5 //String Handlers6 //The console reads a string containing only ' 0 ' ~ ' 9 ' and ' a ' ~ ' Z ' of the 36 ASCII characters, re-sorts the characters in the string, and divides the input string into multiple segments. 7 //8 //(1) The characters in each paragraph should be in strict ascending order. The sorting criterion here is the ASCII code value according to the character (e.g. ' 9 ' is greater than ' 0 ', ' z ' is greater than ' a ')9 //(2) The characters in the 2nd paragraph must be a subset of the characters in the 1th paragraph (which can be the same), the characters in the 3rd paragraph must be a subset of the characters in the 2nd paragraph, and so on, the character of paragraph I must be a subset of the characters in paragraph i-1. Ten //(3) direct termination when the program reads illegal characters One  Public classTester { A  -     Private Final Static int[] Count_array =New int[36];//The corresponding number of 36 ASCII code characters -  the      Public Static voidMain (string[] args) { -  -Getcountarray ();//Get String -ReSort (Count_array);//Processing Output +  -     } +  A     Private StaticString getString () {//Enter a string from the console at  -Scanner reader =NewScanner (system.in);//read in pending string -String input = Reader.next ();//Convert a string into a character array -         returninput; -     } -  in     Private Static voidGetcountarray () {//get an array of strings, iterate over the array and store each character repeat number in the character count array -  to         Char[] Array =getString (). ToCharArray (); +  -          for(CharC:array) {//count the occurrences of each character the  *             if(Isnumber (c)) {//if the number $Count_array[c-' 0 ']++;Panax Notoginseng}Else if(Ischaracter (c)) {//if the character -Count_array[c-' a ' + 10]++; the}Else { +System.out.println ("Input string is illegal"); ASystem.exit (0); the             } +  -         } $     } $  -     Private Static voidReSort (int[] arr) {//processing strings as required -StringBuffer SB =NewStringBuffer (); the          while(!Isallzero (arr)) { - Wuyi              for(inti = 0; i < arr.length; i++) { the                 if(Arr[i] > 0) { -Sb.append (Getcharacter (i));//get the corresponding character of position I Wuarr[i]--; -                 } About  $             } -         } - System.out.println (SB); -  A     } +  the     Private Static CharGetcharacter (inti) {//get the corresponding position (0-35) Character processing function -         //TODO auto-generated Method Stub $         CharA = ' '; the         if(I >= 0 && i <= 9) theA = (Char) (i + ' 0 ')); the         if(I >= && i <= 35) theA = (Char) (i + ' a '-10); -         returnA; in     } the  the     Private Static BooleanIscharacter (Charc) {//determines whether a character is About  the         if(c >= ' a ' && c <= ' Z ' | | c >= ' A ' && c <= ' z ') the             return true; the         return false; +     } -  the     Private Static BooleanIsnumber (Charc) {//determine whether a numberBayi         if(c >= ' 0 ' && C <= ' 9 ') the             return true; the         return false; -     } -  the     Private Static BooleanIsallzero (int[] arr) {//control array Loop traversal the         inttemp = 0; the          for(inti = 0; i < arr.length; i++) { the             if(Arr[i] = = 0) -temp++; the         } the         if(temp = =arr.length) the             return true;94         return false; the  the     } the 98}

Operation Result:

Input:

Abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee

fadfa1301341

#Rda

Output:

Abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa

0134adf13af1

Invalid input string

The problem has all been resolved.

String string processing

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.