In pseudo code (well, Python actually, but it s as close to pseudo code as a real language gets):
def recurse (pref,suff):
# If no characters left, just print prefix.
if suff == "":
print pref
return
# Otherwise add lowercase of first suffix letter to prefix
# and recur with that and the remainder of the suffix.
# Then do the same for uppercase.
# If you wanted smarts, this is where you d detect if the
# upper and lower were the same and only recurse once.
recurse (pref + suff[0:1].lower(), suff[1:])
recurse (pref + suff[0:1].upper(), suff[1:])
# Test the function with "Ben".
recurse ("","beN")
This outputs:
ben
beN
bEn
bEN
Ben
BeN
BEn
BEN
The way it works is relatively simple (most recursive solutions are once you understand them).
The starting condition is when you have no prefix and a suffix that you need to iterate over to get all the different possibilities of mixed case.
The terminating condition is simply when there s no letters left to select two cases for.
Every other condition you simply recur twice, once with the lower case letter and once with the upper.
Keep in mind that this will not handle non-alpha characters properly, it s only meant to show you how the logic works. For example for the string "a!", you will get four lines of output even though "!" is the same in upper and lower case.
For proper handling of that, you would use:
def recurse (pref,suff):
# If no characters left, just print prefix.
if suff == "":
print pref
return
# Otherwise add lowercase of first suffix letter to prefix
# and recur with that and the remainder of the suffix.
# Then do the same for uppercase.
# We also detect if the upper and lower are the same
# and only recurse once.
if suff[0:1].lower() == suff[0:1].upper():
recurse (pref + suff[0:1], suff[1:])
else:
recurse (pref + suff[0:1].lower(), suff[1:])
recurse (pref + suff[0:1].upper(), suff[1:])
# Test the function with "Ben!!!".
recurse ("","beN!!!")
which only gives 8 lines instead of 64.
The equivalent in Java, since this isn t homework, is:
public class test {
public static void recurse (String pref, String suff) {
if (suff.length() == 0) {
System.out.println (pref);
return;
}
String first = suff.substring(0,1);
String rest = suff.substring(1);
if (first.toLowerCase().equals(first.toUpperCase())) {
recurse (pref + first, rest);
} else {
recurse (pref + first.toLowerCase(), rest);
recurse (pref + first.toUpperCase(), rest);
}
}
public static void main(String[] args) {
recurse ("","beN!!!");
}
}