Okay I need to find a way to mix up a word (a String) recursively. I have just been trying some stuff:

import java.util.Random;
public class JumbleRecursive
{
    public static void main(String args[ ])
    {
        String word = "test";
        System.out.println(jumbleRecursive(word));
    }

    public static String jumbleRecursive(String word)
    {
        String a = null;
        String newWord = "";
        char temp;
        int swapWith;
        if (word.length() == 1)
        {
            return word;
        }
    
        else
        {
    
            //String lastChar = word.substring(word.length()-1,word.length());
        
            //String remainingString = word.substring(0, word.length() -1);
            
            swapWith = (int) (Math.random() * word.length());
            //newWord.setCharAt(0, newWord.charAt(swapWith));
            //newWord.setCharAt(swapWith, temp);
            
            a = jumbleRecursive(word);
            return a;
        }
    }
}

I got it iteratively:

import java.util.Random;
public class JumbleIterative
{
    public static void main(String args[ ])
    {
        String word = "test";
        System.out.println(jumbleIterative(word));
    }

    public static String jumbleIterative(String word)
    {
        StringBuilder newWord = new StringBuilder(word);
        char temp;
        int swapWith;
        for(int i = 0; i < word.length(); i++)
        {
            temp = newWord.charAt(i);
            swapWith = (int) (Math.random() * newWord.length());
            newWord.setCharAt(i, newWord.charAt(swapWith));
            newWord.setCharAt(swapWith, temp);
        }    
        return newWord.toString();
    }
}

but I am really stuck on recursive. How do I mix the letters and
call the jumbleRecursive method as well? I am really trying and if you could just write the recursive part, it would make my week!! I normally do not try to get others to do my homework, but I am in a real bind. Please find it in you to help me!!!

Recommended Answers

All 6 Replies

The best thing to do here is to just take your iterative solution, assuming it works, and asking yourself how you can make it recursive. I'm assuming that you have to randomly jumble the letters with each other, and that setCharAt() method does that when two indecies are given. With your iterative solution, you went through a char at a time and swapped with another in the sentence. The recursion would be the same way, except you have to break the word into smaller parts until you get to one char, then work back up and jumble it. So it works like a recursive min or max function.

Length of the string you pass down the recusive call chain won't change (should not change).
What should change is "what is changing in your for loop in iterative model" viz. index of character on which you're working.
So pass the index of character on which the function is supposed to work along with String on which it has to work. Inside the function the condition would be to check if this index has reached it's limit (0 or string.length() depending on whether you're recursing to or fro the end of string) instead of "word.length() == 1"

public class Main
{
    public static void main(String args[ ])
    {
        String word = "abcdefghijklmnopqrstuvwxyz0123456789";
        System.out.println(jumbleIterative(word));
        System.out.println(jumbleRecurse(word, word.length()));
    }

    public static String my_swap( String str, int to, int from ) {
        StringBuilder newWord = new StringBuilder(str);
        char temp = newWord.charAt(from);
        newWord.setCharAt(from, newWord.charAt(to));
        newWord.setCharAt(to, temp);
        return newWord.toString();
    }

    public static String jumbleIterative(String word)
    {
        for(int i = 0; i < word.length(); i++)
        {
            word = my_swap(word, (int) (Math.random() * word.length()), i) ;
        }
        return word;
    }
    
    public static String jumbleRecurse( String str, int index ) {

        if( index != 0 ) {
            str = jumbleRecurse( str, --index ) ;
        }
        return my_swap ( str, (int) (Math.random() * str.length()), index ) ;
    }
}

THANK YOU SO MUCH! YOU DONT KNOW HOW MUCH THAT SAVED ME!!! One last thing though, is there a way to delete this thread? I do not want it to be searchable on google. Thanks!!!!!!!

is there a way to delete this thread?

You, I doubt it.. in any case Moderator can do it and I hope he doesn't for other sake.. sorry..

Member Avatar for iamthwee

And that's why you shouldn't do other people's homework.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.