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

后端 未结 26 2820
一个人的身影
一个人的身影 2020-11-27 14:56

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 cam

26条回答
  •  借酒劲吻你
    2020-11-27 15:47

    This is what I've found to work and use recursive. You can pass str.length() as strLength argument

    private static String reverse(String str, int strLength) {
        String result = "";
        if(strLength > 0)
            result = str.charAt(strLength - 1) + reverse(str, strLength - 1);
        return result;
    }
    

提交回复
热议问题