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!!!