1. String sorting

1. String sorting

Title description

Write a program to sort 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 upper and lower case of the same English letter exist at the same time, they are arranged in the order of input.

For example, input: BabA output: aABb

Rule 3: Other characters other than English letters keep their original positions.

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

Example

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

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

analysis

1. First, the keyboard receives a string of characters, you can use BufferedReader or Scanner

2. It is not case sensitive, so the judgment is repeated 26 times,

3. First filter the letters, sort the letters, if you enter the same letters such as A and a in the order of input, in other words, you don’t need to deliberately consider the capitalization issue, use StringBuild for splicing, if the splicing meets the requirements Later, the sorting is implemented

4. Insert the non-letter into the string and insert it into the original position.

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str;
		while ((str = br.readLine()) != null) {
			char[] array = str.toCharArray();
			StringBuilder ss = new StringBuilder();
			for (int i = 0; i < 26; i++) {
				char c = (char) ('A' + i);
				for (int j = 0; j < str.length(); j++) {
					if (c == array[j] || c == array[j] - 'a' + 'A') {
						ss.append(array[j]);
					}
				}
			}
			for (int i = 0; i < array.length; i++) {
				if (!((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z'))) {
					ss.insert(i, array[i]);
				}
			}
			System.out.println(ss.toString());
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_45874107/article/details/113619983