1public class ReverseString {
2 public static void main(String[] args) {
3 String s1 = "neelendra";
4 for(int i=s1.length()-1;i>=0;i--)
5 {
6 System.out.print(s1.charAt(i));
7 }
8 }
9}
1// Library approach
2public static String solution(String str) {
3 // StringBuilder is not thread-safe, use StringBuffer
4 return new StringBuffer(str).reverse().toString();
5}
6
7// DIY approach
8public static String solution(String str) {
9 char[] chars = str.toCharArray();
10 for(int i = 0, j = str.length() - 1; i < j; i++, j--) {
11 char ch = chars[i];
12 chars[i] = chars[j];
13 chars[j] = ch;
14 }
15 return new String(chars);
16}
1String string="whatever";
2String reverse = new StringBuffer(string).reverse().toString();
3System.out.println(reverse);
4
1// reverse a string using reverse() method of StringBuilder class
2class ReverseUsingReverseMethod
3{
4 public static void main(String[] args)
5 {
6 String str = "Hello world Java";
7 StringBuilder sb = new StringBuilder();
8 // append string to StringBuilder
9 sb.append(str);
10 sb = sb.reverse();
11 // printing reversed String
12 System.out.println(sb);
13 }
14}