forum

Home / DeveloperSection / Forums / What is the finest method to recursively reverse a string in Java?

What is the finest method to recursively reverse a string in Java?

Anonymous User172304-Oct-2013

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:

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));
    }


 


Updated on 04-Oct-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By