How to Reverse Words In a String

In this example, we’ll find how to reverse words in a string

Input:
String word = "hello world";

Output:
world hello

1. Java

public class ReverseWordsInString {

	public static void main(String[] args) {
		String sentence = "Java is Great";
		String reversed = "";
		String[] split = sentence.split(" ");

		for (int i = split.length-1; i >= 0; i--) {
			reversed += split[i] + " ";
		}
		System.out.println(reversed);

	}

}

Output:

Great is Java 

Complete Java Solutions can be found here: Github Link

The complete list can be found here: Practise Programming Questions

Leave a Comment