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

后端 未结 26 2890
一个人的身影
一个人的身影 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条回答
  •  旧时难觅i
    2020-11-27 15:29

    public static String rev(String name){
        if(name.length()>=1){
        System.out.print(name.charAt(name.length()-1)); 
        return rev(name.substring(0,name.length()-1));
        }
        else{
            return ""+name.substring(0);
        }
    }
    

提交回复
热议问题