1009. The irony (20)
time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue
Given a sentence of English, you are asked to write a program to output all the words in the sentence in reverse order.
Input format: The test input contains a test case giving a string with a total length of no more than 80 in one line. The string consists of several words and several spaces. The word is a string composed of English letters (case-sensitive). The words are separated by a space. The input ensures that there are no extra spaces at the end of the sentence.
Output format: The output of each test case occupies one line, and the sentences in reverse order are output.
Input sample:Hello World Here I Come
Sample output:
Come I Here World Hello
-----------------------------------------------------------------------------------------------------------------
/*Idea: Use the trim() method to remove the leading and trailing spaces from the input string, * Then use the split() method to divide the string into multiple character substrings with spaces as the boundary, and save them to the string array * The trim() and split() methods are both object methods of the String class. * Last traversal output in reverse order * */ import java.util.Scanner; public class PAT_B_1009 //The class name is changed to Main { public static void main(String[] args) { Scanner in = new Scanner(System.in);//Receive input string String[] words = in.nextLine().trim().split(" "); //Use the trim() method to remove the leading and trailing spaces from the input string, / / Use the split() method to divide the string into multiple character substrings with spaces as the boundary, and save them to the string array for(int i = words.length-1; i >= 0 ; i--) {//Reverse order traversal, formatted output if(i != words.length-1) System.out.print(" "); System.out.print(words[i]); } } }