Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
255 Members
Join this group to post and comment.
Information Technology
08 Feb 2019

How to reverse a sentence word by word in java?

Step 1 : Create one java.util.Scanner object to take input from the user.

Scanner sc = new Scanner(;

Step 2 : Take inputString from the user.

String inputString = sc.nextLine();

Step 3 : Split inputString into words and store them in a string array.

String[] words = inputString.split(“\\s”);

Step 4 : Create one empty outputString object.

String outputString = “”;

Step 5 : Starting from last word of words array, append each word to outputString.

for (int i = words.length-1; i >= 0; i–)
        outputString = outputString + words[i] + ” “;

Step 6 : Print outputString.

System.out.println(“Output String : “+outputString);

Step 7 : Close the resources.


If you follow the above algorithm then you will able to complete the reverse of a sentence.

Input   :   Java Concept Of The Day

Output   :   Day The Of Concept Java