PAT (Basic Level) 1009 ironic (20 points) JAVA solution

Given a word of English, asking you to write a program, all the words of the sentence order reversed output.

Input formats:

Test input comprises a test case, given the string length does not exceed a total of 80 in a row. String composed of several words and a number of spaces, where the word is English letters (case is case) consisting of string, separated by a space between words, the input end of the sentence to ensure that no extra space.

Output formats:

Each test case output per line, output sentence after the reverse.

Sample input:

Hello World Here I Come

Sample output:

Come I Here World Hello



import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		System.out.println(reverse(s));
		

	}

	private static String reverse(String src) {
		String a = reverseString(src);
		//切割单词
		String[] words = a.split("\\s");
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < words.length; i++) {
			sb.append(reverseString(words[i])+" ");
		}
		return sb.deleteCharAt(sb.length()-1).toString();
		
	}

	private static String reverseString(String src) {
		StringBuilder sb = new StringBuilder(src);
		String re = sb.reverse().toString();
		return re;
	}

}

Here Insert Picture Description

Published 82 original articles · won praise 1 · views 1001

Guess you like

Origin blog.csdn.net/qq_44028719/article/details/104310313