Write a program to reverse the String in java ?
There are many ways to reverse String in Java. We can reverse String using StringBuffer, StringBuilder, iteration etc. Let's see the ways to reverse String in Java.
1. By Reverse Iteration
public class StringFormatter {
public static String reverseString(String str){
char ch[]=str.toCharArray();
String rev="";
for(int i=ch.length-1;i>=0;i--){
rev+=ch[i];
}
return rev;
}
}
public class TestStringFormatter {
public static void main(String[] args) {
System.out.println(StringFormatter.reverseString("my name is khan"));
System.out.println(StringFormatter.reverseString("I am sonoo jaiswal"));
}
}
OUTPUT
nahk si eman ym
lawsiaj oonos ma I