QnA | How do I reverse a String/Sentence in Java?

Question: Print Hello World as ‘Olleh Dlrow’ using Java.

Print Reverse of a String


Write a program to reverse a String or we can say that reverses each word in a sentence and also while printing the first letter should be in upper case. Input can contain any number of words as mentioned below:

Input String:- 
Hello World
ask for help
You Live Only Once

The output would be as:-
Olleh Dlrow
Ksa Rof Pleh
Uoy Evil Ylno Ecno

Code to Reverse a String

public class ReverseWord 

	public static void main(String[] args) 
		// Step #1: Taking multiple Strings in array.
		String[] strArray={"Hello World","ask for help","You Live Only Once"};
		for (String string : strArray) 

	public static void reverseString(String string)
		// Step #2: Extracting words from a sentence.
		String[] getWords=string.split(" ");
		for (String word : getWords) 
			// Step #3 : Getting reverse of each word.
			String revString=new StringBuffer(word).reverse().toString();
			// Step #4: Creating first letter in Upper case and rest in Lower case for each word.
			revString=revString.substring(0, 1).toUpperCase()+revString.substring(1).toLowerCase();
			// Step #5: Printing the expected Output
			System.out.print(revString+" ");

Java Question And Answer:


Computer Basics:

Java Basics:

Java File Handling:

OOPs Concept:

Java Programs:

Selenium Tutorials:



Leave a Comment

Your email address will not be published. Required fields are marked *

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.