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--)
		{
			System.out.print(text.charAt(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();
		System.out.println(reverseString);
	}
}

TestNG:

Computer Basics:

Java Basics:
Java Collection:

Java File Handling:

OOPs Concept:

Java Question And Answer:

Java Programs:

Selenium Tutorials:

JIRA:

Agile:


1532total visits,9visits today

Leave a Reply

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