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 6 of 6

Thread: Print a string in reverse using recursion

  1. #1
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Print a string in reverse using recursion

    Here's my code for printing a string in reverse. It works perfectly.

    I printed out the string normal and not in reverse no problem. I then just switched the else statement around and it worked. Great huh? But I have no idea how it prints the letter o first then the rest.

    Could someone explain it to me?

    Much appreciated.



    static void printReverse(String s){
    		if(s==null || s.equals("")){
    			return;
    		}
    		else{ 
    			printReverse(s.substring(1));        //  print in reverse
    			System.out.println(s.charAt(0));
     
                            // print normal char per line
                            // System.out.println(s.charAt(0));
                            // printReverse(s.substring(1));
     
    		}
    }
     
    public static void main(String[] args) {
     
           printReverse("hello");
     
    }
    	/*
    		output is -
    		o
    		l
    		l
                    e
    		h
    	*/


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print a string in reverse using recursion

    Try debugging the code by adding lots of printlns that show execution flow and what the args arg that are passed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Print a string in reverse using recursion

    System.out.println(s.charAt(0));

    The problem is right here. You're printing it wrong.

  4. #4
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Print a string in reverse using recursion

    It's doing exactly what the question asks. What would be an alternative print?

    I know the way a substring works, with printing the rest of a string after the index number but how does the letter o come out first in the recursion?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Print a string in reverse using recursion

    how it prints the letter o first then the rest.
    When is the println statement executed? Manually work through the code (or add some println statements) to trace the order of the execution of the statements in the program.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    ManInTheMiddle (December 1st, 2012)

  7. #6
    Member
    Join Date
    Nov 2012
    Posts
    49
    My Mood
    Amused
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Print a string in reverse using recursion

    Cheers man. Will do.

Similar Threads

  1. Reverse a String
    By Mehwish-S in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 22nd, 2012, 09:03 AM
  2. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  3. Print 000 infront of String, I can get INT to work but not string
    By keat84 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2012, 11:23 PM
  4. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  5. Replies: 3
    Last Post: June 14th, 2009, 09:31 PM