1.

How to reverse a string in Java?

Answer»

There are different ways to reverse a STRING in Java-like using CharAt() method, StringBuilder/StringBuffer Class, Reverse Iteration, ETC. The reverse() method is AVAILABLE in both the StringBuilder and StringBuffer classes and is commonly used to reverse a string. The reverse () method simply reverses the order of the characters. Below is a Java program to reverse a string using the StringBuilder class.

public class ReverseString{ // function to reverse a string using StringBuilder public static String revstr(String str) { return new StringBuilder(str).reverse().toString(); } public static VOID main(String[] args) { String str= "Scaler by InterviewBit"; str= revstr(str); System.out.println("Result after reversing a string is: "+ str); }}

Output:

Result after reversing a string is: tiBweivretnI YB relacS


Discussion

No Comment Found