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-24-2007, 04:54 PM
zoe zoe is offline
Member
 
Join Date: Jul 2007
Posts: 40
zoe is on a distinguished road
Recursive Anagram
I am having trouble wrapping my head around the concept of recursion. I get how it is implemented, though I have trouble when asked to code anything using the technique. Here is my problem:

I need to write a recursive method that takes a string variable entered by a user and displays all anagrams of that word (i.e. entering cat would display "Cat, Cta, atC, aCt, tCa, taC"). I have been instructed that the method should take in two paramters, prefix and suffix, both strings. The whole word, the first time the method will be called, will be the suffix, while the prefix will be empty. Each recursive call will increment the prefix by one letter, decrement the suffix. I suppose the point is that the suffix gets smaller and smaller and when it reaches 1 letter in length, the recursion stops. Somehow the letters should be rearranged in every possible order, but I can't figure it out. Here is what I have so far:
Code:
public static void anag(String prefix, String suffix) { if (suffix.length()==1) System.out.println(prefix+suffix); else { prefix=suffix.charAt(0)+prefix; for(int x=0; x<suffix.length(); x++) { suffix=suffix.substring(1); suffix=suffix.charAt(suffix.length()-1)+suffix.substring(1); anag(prefix,suffix); } } }
I don't know what to do in the slightest. I don't know how to approach it. Any tips? Disregard the code snippet if it is totally wrong. I was just trying my hand at incrementing and decrement the strings, possibly rearranging the letters, but it doesn't work very well at all.
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 08:15 AM
Member
 
Join Date: Jul 2007
Posts: 40
fernando is on a distinguished road
Three things to remember when you are doing a recursive method.

The method should return a value type, and it should have a recursive case, and a non recursive case.

Although elegant, recursion can be a doozy to wrap your head around

The following is not necessarily correct, but should get you thinking on the right track.

Code:
String anagram(String suffix, String prefix) { if (prefix == null) return; for (int i = 0; i < suffix.length() ; i++) { System.out.println(anagram(<mess with prefix suffix here>)); } }
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
exercise of recursive method amexudo New To Java 2 03-09-2008 07:55 PM
Recursive Method bluegreen7hi New To Java 5 11-29-2007 06:45 AM
Help with recursive implementation toby Advanced Java 1 08-07-2007 07:57 AM
problem with recursive binary search program imran_khan New To Java 3 08-02-2007 05:08 PM
Help with recursive function in java cachi Advanced Java 2 07-31-2007 08:51 PM


All times are GMT +3. The time now is 11:40 PM.


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