Results 1 to 5 of 5
  1. #1
    EatMyChidori is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Help with Strings

    So I am trying to write a code that will take in a string and print its exact reverse.

    My teacher taught me how to do it with recursion today but i don't really understand it.
    Can someone explain this code to me?

    Java Code:
    public String reverse(String s) {
        if (s.length() <= 1) { 
            return s;
        }
        return reverse(s.substring(1, s.length())) + s.charAt(0);
    }
    I'm having trouble understanding the last line.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,121
    Rep Power
    20

    Default Re: Help with Strings

    That's the recursion part.
    Do you know what these two parts do?
    substring(1, s.length())
    and
    s.charAt(0)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    EatMyChidori is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help with Strings

    s.charAt(0); returns the first letter. If i input BLACK it would return B

    substring(1, s.length()) i don't really understand. It returns the first letter to the length of letters?

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Help with Strings

    It returns the first letter to the length of letters?
    Close. You noted in the first line that chartAt(0) is the first letter, so substring(1... would be the second letter right?

    As always, try it and see! Use your example of the word BLACK and see what you get!

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,121
    Rep Power
    20

    Default Re: Help with Strings

    Java Code:
    public String reverse(String s) {
        System.out.println("reverse " + s);
        if (s.length() <= 1) { 
            return s;
        }
        System.out.println("charAt(0) " + s.charAt(0));
        return reverse(s.substring(1, s.length())) + s.charAt(0);
    }
    If you have a way of running the code as a test (which shouldn't be difficult) try the above.
    This should show you what each call to reverse is doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need help with strings please!!!!
    By ndsmith20 in forum New To Java
    Replies: 4
    Last Post: 10-25-2012, 12:24 AM
  2. Strings
    By Krazzyjman in forum New To Java
    Replies: 3
    Last Post: 09-07-2012, 09:14 PM
  3. need help with strings
    By iron_fist in forum Java Applets
    Replies: 10
    Last Post: 02-15-2012, 06:16 PM
  4. Need help with Strings
    By </3java in forum New To Java
    Replies: 24
    Last Post: 02-17-2011, 06:00 PM
  5. It is possible in Strings..?
    By mlibot in forum New To Java
    Replies: 1
    Last Post: 03-12-2010, 06:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •