Different ways to Reverse a String in Java.

We can reverse a String via characters as well as with words. There are multiple ways to achieve it.

  • Below are the methods to do do the same:
    • converting a string into an array to reverse by characters
    • Splitting String with blank spaces to reverse by words
    • Using the reverse() method from the StringBuffer class
Reverse of String in Java.

Reverse a String

package Programs;
public class reverseString {
	public static void main(String[] args) 
		String text="ALL IN ONE BLOGS";
		System.out.println("Actual String: \n"+text);
		int length=text.length()-1;
		System.out.println("\nReverse by Charatcters: ");
		for(int x=length;x>=0;x--)
		String[] textArray=text.split(" ");
		int arrayLen=textArray.length-1;
		System.out.println("\n\nReverse by Words: ");
		for(int x=arrayLen;x>=0;x--)
			System.out.print(textArray[x]+" ");
		System.out.println("\n\nReverse String using in-build method:");
		StringBuffer sb=new StringBuffer(text);
		StringBuffer reverseString=sb.reverse();


Computer Basics:

Java Basics:
Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:



Leave a Reply

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

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 51,167
  • Total page view: 70,434

   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.