Shorter. It is assumed that the input string consists of upper and
lower case letters only.
void allCase(string r)
{
int i, n = s.sise();
for( i = 0 ; i < (1<<n) ; ++i )
{
string[i^(i>>1)] ^= 'a' ^ 'A';
cout << s << endl;
}
}
The expression i^(i>>1) is a Gray-code (see
http://en.wikipedia.org/wiki/Gray_code)
conversion. In Gray code, the numbers from 0 to 2^n - 1 are arranged
in an order so that only one bit changes from number to number. So
this code changes the case of one character at a time, and after 2^n
iterations, has printed all of the combinations of cases.
Dave
On Sep 14, 5:39 am, anshu mishra <[email protected]> wrote:
> void allCase(string r)
> {
> int n = s.sise();
> string s;
> for (i = 0; i < (1 << n); i++)
> {
> s = r;
> for ( j = 0; j < n; j++)
> {
> if ( i & ( 1 << j) )
> {
> s[j] = s[j] + ('a' - 'A');
> }
> }
> cout << s << endl;
> }
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.