Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Recursive method that accepts a single string as a parameter and returns the reverse of that String

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Recursive method that accepts a single string as a parameter and returns the reverse of that String

    write a recursive method that accepts a single String as a parameter and returns the reverse of that String. For example, calling the method reverse("Test would return the String "tesT".

    any suggestion?

    Thanks in advance


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: any one can solve this problem?

    String example = "Hello, World!";
    System.out.println( (new StringBuffer(example)).reverse() );

    That should get you going You will need imports etc too hehe xD

    P.S Sorry for the slow reply
    Regards,
    Chris

  3. #3
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: any one can solve this problem?

    hi,

    wlecome to the java prog forums.

    here is the code: http://www.javaprogrammingforums.com...e-program.html

    above is the sample code.

    Cheers.
    Truffy
    Last edited by Truffy; June 14th, 2009 at 09:37 PM.

  4. #4
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: any one can solve this problem?

    Quote Originally Posted by eason36 View Post
    write a recursive method that accepts a single String as a parameter and returns the reverse of that String. For example, calling the method reverse("Test would return the String "tesT".

    any suggestion?

    Thanks in advance
    Public String someMethod(String input)
    {
     String output="";
     Char[] reverse=new Char[input.length()];
     reverse=input.tochararray();
    for(int i=input.length();i>=0;i--)
    {
    output+=Character.toString(reverse[i])
    } 
    return output;   
    }

    keep in mind this is a skeleton method and most likely doesn't work due to the fact Im not in front of an IDE to test this out in . i just wanted you to get the idea. Oh this is not recursive btw.(sorry).
    Last edited by Fendaril; June 14th, 2009 at 11:02 PM. Reason: mistake.

Similar Threads

  1. Java error "java.lang.StackOverflowError"
    By sanatkumar in forum Exceptions
    Replies: 2
    Last Post: July 7th, 2009, 02:40 PM
  2. Replies: 4
    Last Post: June 10th, 2009, 01:04 AM
  3. Replies: 2
    Last Post: May 16th, 2009, 05:23 AM

Tags for this Thread