Friends, I have a major problem with recursion. My question is how do you reverse a string in java by applying recursion. It's quite easy working wthout recursion by using using StringUtils.reverse, but I really really need help with recursion on this question.!!!!!!Please!!!!!

Recommended Answers

All 5 Replies

1. remove the last character from the input string
2. create a new string consisting of that last character
3. to the new string, append a recursive call of your function with the rest of the input string minus the last character

Note that you will need to figure out the stopping condition of the recursion.

@kramerd, can u plz clarify thru pseudocode or code since m solvin 10 questions and kinda messed up with all the programming methods.
Thank you in advance

I gave you an algorithm so you can write the code yourself. Do you know how to do any of the steps I suggested? If so, give it a try and then ask a more specific question if you get stuck. I don't want to write the code for you because then you won't learn anything.

The key to solving this problem is understanding recursion (or a stack). If you don't understand this, do some reading on it. My initial post there are lots of links that try to explain how recursion works.

As kramered noted you need to keep removing the starting character of a string until there are no more characters to remove.

For example to reverse the String "Help" you would follow this pseudo code:

While more characters exist in string
remove first character and add it to another string

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.