3 ways to Reversing String In JAVA

This is the most asked question in Java programming interviews, and there are many ways to do it

Daily Debug
Women in Technology
1 min readAug 10, 2023

--

Using Java’s String Buffer’s reverse method:

String str = "Reverse This String";
StringBuffer str1 = new StringBuffer(str);
System. out.println("Reversed String : "+str1.reverse());

Although we can use inbuilt Java methods to reverse the string, it is good to know alternatives of it, so let’s check other ways to reverse a string without using inbuilt functions

2. Reverse String using for loop and character array

This is the traditional way of reversing a string in which we traverse through the array backwards.

String str = "Reverse This String";

3. Reverse String using reduce function

String str = "Reverse This String";

It’s also common in interviews that they ask to reverse a sentence without disturbing its sequence.

For example Input — Reverse this string. Output — esreveR sihT gnirtS

String str = "Reverse This String";
String words[] = str.split(" ");
StringBuffer strReversed = new StringBuffer();

Give a clap and follow if this article has helped you learn 😊

--

--

Daily Debug
Women in Technology

I am software engineer, Tech blogger, here to help you learn and share my insights on various tech topics.