sentence reverse order one

sentence reverse order

Description
Arrange an English sentence in reverse order by word. For example, "I am a boy", after the reverse order is "boy a am I",
all words are separated by a space, and the sentence does not contain other characters except English letters

Note that this question has multiple sets of inputs

Enter description :
Enter an English sentence, each word is separated by a space. Ensure that the input contains only spaces and letters.

Output description:
Get sentences in reverse order

Example 1

输入:
I am a boy
输出:
boy a am I

Example 2

输入:
nowcoder
输出:
nowcoder

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) throws Exception {
    
    
        Scanner sc = new Scanner(System.in);

        while (sc.hasNext()){
    
    
            String line = sc.nextLine();
            String[] words = line.split(" ");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = words.length - 1; i >= 0 ; i--){
    
    
                stringBuilder.append(words[i] + " ");
            }
            System.out.println(stringBuilder);


        }


    }

}

Guess you like

Origin blog.csdn.net/huhuhutony/article/details/121173802