So I'm having a little trouble with the final part of my assignment on recursion. The method needs to use recursion to return a string that is formed from "weaving" together two strings taken in as parameters. For example:
weave("aaaa", "bbbb") // should return the string "abababab"
weave("hello", "world") // should return the string "hweolrllod"
weave("recurse", "NOW") // should return the string "rNeOcWurse"
Note that the extra characters from the first string—the characters in "urse"—come after the characters that have been woven together.
The important (and annoying) thing is that I'm not allowed to use any iteration loops (for, while, do while).
Here is my code thus far:
public static String weave(String str1, String str2)
{
String word = str1 + str2;
if(str1 == null || str1.equals("") || str2 == null || str2.equals(""))
{
return word;
}
String word1 = weave(str1.substring(0, str1.length() - 1), str2.substring(0, str2.length() - 1));
System.out.println(word1);
return word;
}
For (Hello, World), my output is:
HW
HeWo
HelWor
HellWorl
HelloWorld
Obviously my characters aren't weaving, so I'm not sure what to do! Also, as stated above, the method should do not printing. I just added in the println statement as a test to see where my program was at.