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}
1// reverse a string using character array
2class ReverseUsingCharacterArray
3{
4 public static void main(String[] args)
5 {
6 String str = "HelloWorldJava";
7 char[] ch = str.toCharArray();
8 for(int a = ch.length - 1; a >= 0; a--)
9 System.out.print(ch[a]);
10 }
11}
1Solution 1
2
3public static String StrReverse(String str) {
4
5String reverse="";
6
7for(int i=str.length()-1; i >= 0; i--)
8
9reverse += str.toCharArray()[i];
10
11
12
13return reverse;
14
15}
16
17
18
19Solution 2 (using string buffer)
20
21public static String Reverse(String str) {
22
23return new StringBuffer(str).reverse().toString());
24
25}
1public class StringReverseExample{
2 public static void main(String[] args) {
3 String string = "abcdef";
4 String reverse = new StringBuffer(string).reverse().toString();
5 System.out.println("\nString before reverse: "+string);
6 System.out.println("String after reverse: "+reverse);
7 }
8}
1// reverse a string using toCharArray() method
2class ReverseUsingToCharArrayMethod
3{
4 public static void main(String[] args)
5 {
6 String str = "Hello World Java";
7 char[] chTemp = str.toCharArray();
8 int left, right = 0;
9 right = chTemp.length - 1;
10 for(left = 0; left < right ; left++, right--)
11 {
12 // swap values
13 char temp = chTemp[left];
14 chTemp[left] = chTemp[right];
15 chTemp[right]=temp;
16 }
17 for(char c : chTemp)
18 System.out.print(c);
19 System.out.println();
20 }
21}
1// reverse a string using ByteArray
2class ReverseStringByteArray
3{
4 public static void main(String[] args)
5 {
6 String input = "HelloWorld";
7 // getBytes() method to convert string into bytes[].
8 byte[] strByteArray = input.getBytes();
9 byte[] output = new byte[strByteArray.length];
10 // store output in reverse order
11 for(int a = 0; a < strByteArray.length; a++)
12 output[a] = strByteArray[strByteArray.length - a - 1];
13 System.out.println(new String(output));
14 }
15}
1// Not the best way i know but i wanted to challenge myself to do it this way so :)
2public static String reverse(String str) {
3 char[] oldCharArray = str.toCharArray();
4 char[] newCharArray= new char[oldCharArray.length];
5 int currentChar = oldCharArray.length-1;
6 for (char c : oldCharArray) {
7 newCharArray[currentChar] = c;
8 currentChar--;
9 }
10 return new String(newCharArray);
11}
1public class Main {
2
3 public static void main(String[] args) {
4 // Write a Java program to reverse a string.
5 Scanner input = new Scanner(System.in);
6 System.out.println("Enter the String:");
7 char[] letters = input.nextLine().toCharArray();
8 System.out.println(" Reverse String");
9 for(int i = letters.length -1; i >= 0; i--){
10 System.out.print(letters[i]);
11 }
12 }
13}
14