Reversing A String Programmatically
Program 1 : Program to Reverse a String (Method – 1 Using for loop)
public class StringReverseProg1 {
// Java Program to Reverse a String Using for loop
public static void main(String[] args) {
String str = "Ksamyatam";
String reverseStr = "";
char ch;
System.out.println("Original String : " + str);
for (int i = 0; i < str.length(); i++) {
ch = str.charAt(i);
reverseStr = ch + reverseStr;
}
System.out.println("Reverse String : " + reverseStr);
}
}
Output :
Original String : Ksamyatam
Reverse String : mataymasK
Program 2 : Program to Reverse a String (Method – 2 Using String.getBytes() Method)
public class StringReverseProg2 {
// Java Program to Reverse a String (Method - 2 Using String.getBytes() Method)
public static void main(String[] args) {
String str = "Ksamyatam";
byte[] arr = str.getBytes();
byte[] resByte = new byte[arr.length];
System.out.println("Original String : " + str);
for (int i = 0; i < arr.length; i++)
resByte[i] = arr[arr.length - i - 1];
String reverseStr = new String(resByte);
System.out.println("Reverse String : " + reverseStr);
}
}
Output :
Original String : Ksamyatam
Reverse String : mataymasK
Program 3 : Program to Reverse a String (Method – 3 Using StringBuilder.reverse() Method)
public class StringReverseProg3 {
// Java Program to Reverse a String Using StringBuilder.reverse() Method
public static void main(String[] args) {
String str = "Ksamyatam";
StringBuilder reverseStr = new StringBuilder();
reverseStr.append(str);
reverseStr.reverse();
System.out.println("Original String : " + str);
System.out.println("Reverse String : " + reverseStr);
}
}
Output :
Original String : Ksamyatam
Reverse String : mataymasK
Program 4 : Program to Reverse a String (Method – 4 Using Character Array)
public class StringReverseProg4 {
// Java Program to Reverse a String Using Character Array
public static void main(String[] args) {
String str = "Ksamyatam";
char[] arr = str.toCharArray();
System.out.println("Original String : " + str);
System.out.println("\nReverse String Method 1 : ");
for (int i = arr.length - 1; i >= 0; i--)
System.out.print(arr[i]);
System.out.println("\n\nReverse String Method 2 : ");
char[] rev = new char[str.length()];
int j = 0;
for (int i = arr.length - 1; i >= 0; i--) {
rev[j] = arr[i];
j++;
}
for (char val : rev)
System.out.print(val);
}
}
Output :
Original String : Ksamyatam
Reverse String Method 1 :
mataymasK
Reverse String Method 2 :
mataymasK
Program 5 : Program to Reverse a String (Method – 5 Using Collections.reverse() Method)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;
public class StringReverseProg5 {
// Java Program to Reverse a String Using Collections.reverse() Method
public static void main(String[] args) {
String str = "Ksamyatam";
char[] arr = str.toCharArray();
List<Character> l = new ArrayList<>();
for (char c : arr)
l.add(c);
Collections.reverse(l);
ListIterator it = l.listIterator();
System.out.println("Original String : " + str);
System.out.println("\nReverse String : ");
while (it.hasNext())
System.out.print(it.next());
}
}
Output :
Original String : Ksamyatam
Reverse String :
mataymasK
Program 6 : Program to Reverse a String (Method – 6 Using StringBuffer.reverse() Method)
public class StringReverseProg6 {
// Java Program to Reverse a String Using StringBuffer.reverse() Method
public static void main(String[] args){
String str = "Ksamyatam";
StringBuffer strBuffer = new StringBuffer(str);
strBuffer.reverse();
System.out.println("Original String : " + str);
System.out.println("Reverse String : " + strBuffer);
}
}
Output :
Original String : Ksamyatam
Reverse String : mataymasK