This is a common question in many of the interview's, let it be a freshers interview or an experienced one!! Employers often look for a solution for building a program using recursion. So, this post has three ways to reverse a String and I hope it would be useful for beginners in Java.
Here goes the code :
package com.fcspoc.java;
public class StringReverse {
public String reverseStringBuffer(String s) {
return new StringBuffer(s).reverse().toString();
}
/* *
* Reverse For Loop: Char Array
*/
public String reverseCharArray(String s) {
char[] reverseStringArray = new char[s.length()];
for (int i = s.length() - 1, j = 0; i != -1; i--, j++) {
reverseStringArray[j] = s.charAt(i);
}
return new String(reverseStringArray);
}
/**
* Reverse For Loop: String Variable
*/
public String reverseStringVariable(String s) {
String reverseStringVariable = "";
for (int i = s.length() - 1; i != -1; i--) {
reverseStringVariable += s.charAt(i);
}
return reverseStringVariable;
}
public String reverseStringRecursion(String s) {
if (s.length() <= 1) {
return s;
}
return reverseStringRecursion(s.substring(1, s.length())) + s.charAt(0);
}
public static String reverseRecursionReverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverseRecursionReverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
StringReverse stringReverse = new StringReverse();
String s = "SAMPLETEST";
String reverseStringBufferVar = stringReverse.reverseStringBuffer(s);
System.out.println("Reverse String - String Buffer value is : "+reverseStringBufferVar);
String reverseCharArrayVar = stringReverse.reverseCharArray(s);
System.out.println("Reverse String - Char Array value is : "+reverseCharArrayVar);
String reverseStringVariableVar = stringReverse.reverseStringVariable(s);
System.out.println("Reverse String - String Variable value is : "+reverseStringVariableVar);
String reverseStringRecursionVar = stringReverse.reverseStringRecursion(s);
System.out.println("Reverse String - String Recursion Ex1 value is : "+reverseStringRecursionVar);
String reverseRecursionReverseVar = reverseRecursionReverse(s);
System.out.println("Reverse String - String Recursion Ex2 value is : "+reverseRecursionReverseVar);
}
}
Here goes the code :
package com.fcspoc.java;
public class StringReverse {
public String reverseStringBuffer(String s) {
return new StringBuffer(s).reverse().toString();
}
/* *
* Reverse For Loop: Char Array
*/
public String reverseCharArray(String s) {
char[] reverseStringArray = new char[s.length()];
for (int i = s.length() - 1, j = 0; i != -1; i--, j++) {
reverseStringArray[j] = s.charAt(i);
}
return new String(reverseStringArray);
}
/**
* Reverse For Loop: String Variable
*/
public String reverseStringVariable(String s) {
String reverseStringVariable = "";
for (int i = s.length() - 1; i != -1; i--) {
reverseStringVariable += s.charAt(i);
}
return reverseStringVariable;
}
public String reverseStringRecursion(String s) {
if (s.length() <= 1) {
return s;
}
return reverseStringRecursion(s.substring(1, s.length())) + s.charAt(0);
}
public static String reverseRecursionReverse(String str) {
if ((null == str) || (str.length() <= 1)) {
return str;
}
return reverseRecursionReverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
StringReverse stringReverse = new StringReverse();
String s = "SAMPLETEST";
String reverseStringBufferVar = stringReverse.reverseStringBuffer(s);
System.out.println("Reverse String - String Buffer value is : "+reverseStringBufferVar);
String reverseCharArrayVar = stringReverse.reverseCharArray(s);
System.out.println("Reverse String - Char Array value is : "+reverseCharArrayVar);
String reverseStringVariableVar = stringReverse.reverseStringVariable(s);
System.out.println("Reverse String - String Variable value is : "+reverseStringVariableVar);
String reverseStringRecursionVar = stringReverse.reverseStringRecursion(s);
System.out.println("Reverse String - String Recursion Ex1 value is : "+reverseStringRecursionVar);
String reverseRecursionReverseVar = reverseRecursionReverse(s);
System.out.println("Reverse String - String Recursion Ex2 value is : "+reverseRecursionReverseVar);
}
}