Frequently Asked Java Program 19: Java Program to Reverse Position of Words in a String

Write a Java program to reverse position of words in given string i.e. word at first position in string should be at last position in string.

Input String: You are awesome
Output String: awesome are You

Solution:

It is a very easy program. You just need understanding of splitting a string, array and for loop to traverse string in reverse direction.

Steps to follow:

Step 1: First of all we need to extract words from a given string. Two words are separated by while space. We can use split() method given by String class which splits a string by provided delimiter and returns a String[] of splitted words. For example:

public class SplitString {

        public static void main(String[] args) {
                
                String inputString = "Make Selenium Easy";
                String spliited[] = inputString.split(" ");
                for(int i=0;i