I have been messing around with recursion today. Often a programming technique that is not used enough.
I set out to recursively reverse a string. Here's what I came up with:
//A method to reverse a string using recursion public String reverseString(String s){ char c = s.charAt(s.length()-1); if(s.length() == 1) return Character.toString(c); return c + reverseString(s.substring(0,s.length()-1)); }
My question: is there a better way in Java?
Explanation: Recursive function (reverse) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way when the pointer reaches '\0', all functions accumulated in stack print char at passed location (str) and return one by one.
Built-in reverse() method Instantiate the StringBuffer class by passing the required String as a parameter. Invoke the reverse() method od the created object. Convert it into String again using the toString() method.
As the stack is involved, we can easily convert the code to use the recursion call stack. As the string is immutable, we first convert the given string into a character array, then reverse the character array and finally convert the character array back into a string.
By Using StringBuilderStringBuilder or StringBuffer class has an in-build method reverse() to reverse the characters in the string. This method replaces the sequence of the characters in reverse order. The reverse method is the static method that has the logic to reverse a string in Java.
Similarly, we can also use recursion to reverse a String in Java. In this section, we will learn how to reverse a string using recursion in Java. The recursive function performs the following steps to reverse a string: First, remove the first character from the string and append that character at the end of the string.
Converting String to character array: The user input the string to be reversed. Method: 1. First, convert String to character array by using the built in Java String class method toCharArray(). 2. Then, scan the string from end to start, and print the character one by one. // Java program to Reverse a String by.
Reverse a String using Recursion To reverse all the characters of the string, we can write a recursive function that will perform the following actions – Program output. 2. Reverse A String using StringBuilder
The best way is not to use recursion. These stuff are usually used to teach students the recursion concept, not actual best practices. So the way you're doing it is just fine. Just don't use recursion in Java for these kind of stuff in real world apps ;) PS.
The best way is not to use recursion. These stuff are usually used to teach students the recursion concept, not actual best practices. So the way you're doing it is just fine. Just don't use recursion in Java for these kind of stuff in real world apps ;)
PS. Aside what I just said, I'd choose ""
as the base case of my recursive function:
public String reverseString(String s){ if (s.length() == 0) return s; return reverseString(s.substring(1)) + s.charAt(0); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With