Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2007, 12:13 AM
Member
 
Join Date: Jul 2007
Posts: 40
lenny is on a distinguished road
Recursion in java
Hello, here's my problem. I must take a string and through recursion display it backwards. Here's what I have thus far...

Code:
import TerminalIO.KeyboardReader; public class StringFun { public static void main(String [] args) { //Variables char runAgain='y'; String string; //Run Again feature while(runAgain=='y'||runAgain=='Y') { //Instantiate a keyboard reader KeyboardReader r=new KeyboardReader(); //User input string=r.readLine("Enter a string: "); System.out.println( "The string '"+string+"' displayed in reverse shows '"+string(string, string.length())+"'"); //Run again option runAgain=r.readChar("Run again (y/n)? "); } } //Recursive method to display the string in reverse static String string(String s, int pos) { if(pos>0) return s; else return string(s, pos-1); } }
Obviously I have something wrong because all I end up with is the string again. Any tips or hints on what I am doing wrong, or not doing?
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 08:23 AM
Member
 
Join Date: Jul 2007
Posts: 40
mathias is on a distinguished road
You should always analyze your algorithms on paper. If you would do this, then you'd see exactly what your function is doing. Use the word "dog" for example, you'd see that each call doesn't change the string at all and just decrements the position variable and all you return in the end is "dog". But this is assuming that you fixed your if statement, because if you look at that, pos is greater than zero on the first call from your main method and doesn't even get to the pseudo-recursive calls. You should look at an example of a recursive algorithm to get an idea on how they're constructed.

I'll give you a quick example of recursion and strings. This example is VERY close to the solution I came up with to create a reverse of a string.
Code:
// This example takes the word and recursively shortens the word by taking // out the first letter and appending that new word to the previous // version of the string. // Ex. word = wordordrdd, shane = shanehaneanenee static String example(String s) { if s.length() > 1 return s + example(s.substring(1,s.length()-1)) else return s }
Hope this gets you on the right track.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help With Recursion andrew777 New To Java 1 03-29-2008 02:51 PM
recursion kdeighan New To Java 3 01-25-2008 11:48 PM
Recursion bozovilla Advanced Java 3 01-07-2008 06:53 PM
recursion ravian New To Java 2 12-03-2007 07:15 PM
Help with recursion scts102 New To Java 1 11-20-2007 12:07 AM


All times are GMT +3. The time now is 04:27 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org