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: String Reversal.... Not working

  1. #1
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default String Reversal.... Not working

    class jack 
    {
    public static String reverseString(String s, String result,int l)
    {
    if(l>=0)
    { result=result+s.charAt(l);
    reverseString(s,result,l-1);
    return result;}
    else 
    return "error";
     
    }
    public static void main(String args[])
    {
    String s="Allen downey";
    String result="";
    int l=s.length();
    String backward=reverseString(s,result,l);
    System.out.println(backward);
    }
    }
    This program should reverse the string
    But The program is not yielding output... I know what you are thinking.... why can't I just use while or for... But I can't... I am supposed to be able to write the program without any iteration...
    please help...


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Thumbs up Re: String Reversal.... Not working

    class jack
    {
    public static String reverseString(String s, String result,int l)
    {
    if(l>=0)
    { result=result+s.charAt(l);
    reverseString(s,result,l-1);
    return result;}
    else
    return "error";

    }
    public static void main(String args[])
    {
    String s="Allen downey";
    String result="";
    int l=s.length();
    String backward=reverseString(s,result,l);
    System.out.println(backward);
    }
    }

    In the above code certain changes are required

    String backward=reverseString(s,result,l);
    Firstly length you are passing should be one less than the actual length because you are directly using length as index value , but here index in a string or array starts from 0 .

    String backward=reverseString(s,result,l);
    String backward=reverseString(s,result,l-1);

    Secondly little modification in this part , while returning value

    if(l>=0)
    { result=result+s.charAt(l);
    reverseString(s,result,l-1);
    return result;}
    else
    return "error";

    change required
    if(l>=0)
    {
    result=result+s.charAt(l);
    return reverseString(s,result,l-1);
    }
    else
    return result;
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. The Following User Says Thank You to DanBrown For This Useful Post:

    moodycrab3 (February 7th, 2011)

  4. #3
    Junior Member moodycrab3's Avatar
    Join Date
    Feb 2011
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: String Reversal.... Not working

    OH MY GOD... u are a genius... Thank you so much... I have been trying to figure this out since yesterday... Thank you so very much....

    thank you
    thank you

  5. #4
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: String Reversal.... Not working

    OH MY GOD... u are a genius... Thank you so much... I have been trying to figure this out since yesterday... Thank you so very much....

    thank you
    thank you

    you r welcome , and please mark this thread as solved.
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

Similar Threads

  1. [SOLVED] Replacing letters in a string NOT WORKING.
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 14th, 2011, 06:38 PM
  2. Replies: 2
    Last Post: December 22nd, 2010, 09:21 AM
  3. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  4. Replies: 2
    Last Post: November 3rd, 2009, 06:28 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM