Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the best way to recursively reverse a string in Java?

Tags:

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?

like image 563
binarycreations Avatar asked May 13 '09 18:05

binarycreations


People also ask

How would you reverse a string recursively?

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.

What is the best way to reverse a string in Java?

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.

How do you reverse a char array using recursion in Java?

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.

Is there any method to reverse a string in Java?

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.

How to reverse a string using recursion 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.

How to reverse string to character array in Java?

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.

How to reverse a string in Python?

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

What is the best way to avoid recursion in Java?

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.


1 Answers

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); } 
like image 193
mmx Avatar answered Sep 16 '22 11:09

mmx