I'm running into an issue with a program. I need a method that takes a string and returns every possible combination of upper and lower case letters for that string. For instance, cat would return:

cat
caT
cAt
cAT
Cat
CaT
CAt
CAT

The string entered could be any length though. I'm having difficulty coming up with an algorithm to do this. Could someone help me get started please?

Recommended Answers

All 3 Replies

Well, you know ahead of time that for a 6 letter word, you have 2^6 = 64 possibilities. You could have the function return an array of Strings. Have a loop that goes from 0 to 63 (or 127, 255, etc.). For your example, "cat", it would be 0 to 7, so you'd need an array of size 8. For the 5th element of the array, take the loop counter variable value (in this case, 5) and convert to binary (no converting needed really, since it's stored that way).

5 is equivalent to 101 in binary. Let 1 be upper case, let 0 be lower case.

So the fifth element in the array would be CaT.

Go through the loop (0 to 7) and you'll get all possibilities.

How would I go about implementing the binary representation of the counter variable?

How would I go about implementing the binary representation of the counter variable?

If you don't write your own function (better do it!), use the built-in, Integer.toBinaryString(int n).

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.