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

后端 未结 26 2538
一个人的身影
一个人的身影 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:29

    If you're writing real code (not learning recursion), use StringBuilder's reverse() method. The Java Tutorial gives this example:

    String palindrome = "Dot saw I was Tod";   
    StringBuilder sb = new StringBuilder(palindrome);
    sb.reverse();  // reverse it
    System.out.println(sb);
    
    0 讨论(0)
  • 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);
        }
    }
    
    0 讨论(0)
  • 2020-11-27 15:31

    If you're going to do this, you want to operate on a character array, because a String is immutable and you're going to be copying Strings all over the place if you do it that way.

    This is untested and totally stream of consciousness. It probably has an OB1 somewhere. And very not-Java.

    public String reverseString(String s)
      {
      char[] cstr = s.getChars();
      reverseCStr(cstr, 0, s.length - 1);
    
      return new String(cstr);
      }
    
    /**
     * Reverse a character array in place.
     */
    private void reverseCStr(char[] a, int s, int e)
      {
      // This is the middle of the array; we're done.
      if (e - s <= 0)
        return;
    
      char t = a[s];
      a[s] = a[e];
      a[e] = t;
      reverseCStr(a, s + 1, e - 1);
      }
    
    0 讨论(0)
  • 2020-11-27 15:35

    There are about 20 answers already but I'll just throw in my recursive algorithm as well. It may be a little verbose but it is at least readable.

    public static String reverseString(String str) {
       return reverseString("", str);
    }
    
    private static String reverseString(String result, String original) {
       if (original.length() == 0) {
          return result;
       } else {
          int length = original.length();
          String lastLetter = original.substring(length - 1, length);
          original = original.substring(0, length - 1);
          return reverseString(result + lastLetter, original);
       }
    }
    

    The code basically recursively takes the end of the string and moves it in front. For example if the string we want to reverse is "jam," then each time the helper method is called, result and original strings are as follows:

    // result:  original:
    // ""       jam
    // m        ja
    // ma       j
    // maj      ""
    
    0 讨论(0)
  • 2020-11-27 15:36

    Here is my immutable version:

     String reverse(String str) {
        if(str.length()<2) return str;
        return  reverse(str.substring(1)) +str.charAt(0);
    }
    

    and tail recursive version:

     String reverseTail(String str) {
        if(str.length()<2) return str;
        return str.charAt(str.length()-1)+ reverseTail(str.substring(0,str.length()-1));
    
    0 讨论(0)
  • 2020-11-27 15:36
    public String reverseString (String s) {
    
        if (s != null && s.length () > 0 ) {
            rev = rev + s.substring (s.length () - 1);
            reverseString (s.substring (0, s.length () - 1));
        }
        return rev;
    
    }
    
    0 讨论(0)
提交回复
热议问题