How to reverse string in java is popular core java interview question and asked on all levels from junior to senior java programming job. since Java has rich API most java programmer answer this question by using StringBuffer reverse() method which easily reverses an String in Java and its right way if you are programming in Java but most interview doesn't stop there and they ask interviewee to reverse String in Java without using StringBuffer or they will ask you to write an iterative reverse function which reverses string in Java. In this tutorial we will see how to reverse string in both iterative and recursive fashion. This example will help you to prepare better for using recursion in java which is often a weak area of Java programmer and exposed during a programming interview.
Reverse String in Java using Iteration and Recursion
if you are from C background and new to Java then you get surprise that java Strings are not character array instead they are object and Strings in Java are not null terminated but still you can use your C skill to write iterative reverse function for string by getting character array by calling String.toCharArray() and getting length of String by calling String.length(). Now if you succeed in writing String reverse function, an iterative version without using StringBuffer reverse than they finally ask you to write a recursive one. Since recursion is a tricky concept and not many Java developer think recursive as compared to C++ dudes, you may see many of Java programmer stuck here by doing doodling around subString, indexOf etc. So its better to prepare in advance. here I have given example of reversing string using StringBuffer, iterative version of String reversal and reversing string in Java using recursion. As I said recursive solution is always tricky to come up you need to found a suitable base case and than repetitive call recursive method with each call reducing length of argument. anyway mine way is just one way of reversing string using recursion, there are many solution out there. so don’t forget to try out yourself.
String Reverse Example using Iteration and Recursion in Java
Here is code example String reverse using iterative and recursive function written in Java. Recursive solution is just for demonstrative and education purpose, don’t use recursive solution in production code as it may result in StackOverFlowError if String to be reversed is very long String or if you have any bug in your reverse function, anyway its good test to make yourself comfortable with recursive functions in java.
import java.io.FileNotFoundException;
import java.io.IOException;
/**
*
*@author Javin
/
public class StringReverseExample {
public static void main(String args[]) throws FileNotFoundException, IOException {
//original string
String str = "Sony is going to introduce Internet TV soon";
System.out.println("Original String: " + str);
//reversed string using Stringbuffer
String reverseStr = new StringBuffer(str).reverse().toString();
System.out.println("Reverse String in Java using StringBuffer: " + reverseStr);
//iterative method to reverse String in Java
reverseStr = reverse(str);
System.out.println("Reverse String in Java using Iteration: " + reverseStr);
//recursive method to reverse String in Java
reverseStr = reverseRecursively(str);
System.out.println("Reverse String in Java using Recursion: " + reverseStr);
}
public static String reverse(String str) {
StringBuilder strBuilder = new StringBuilder();
char[] strChars = str.toCharArray();
for (int i = strChars.length - 1; i >= 0; i--) {
strBuilder.append(strChars[i]);
}
return strBuilder.toString();
}
public static String reverseRecursively(String str) {
//base case to handle one char string and empty string
if (str.length() < 2) {
return str;
}
return reverseRecursively(str.substring(1)) + str.charAt(0);
}
}
That’s all on how to reverse String in Java using Recursion, Iteration and without using StringBuffer. This is one of my favorite interview question and I mostly ask to write recursive solution just to know whether programmer has ability to comprehend recursive problem or not.
Here are few more Java interview questions you may like.
Tidak ada komentar:
Posting Komentar