# Permute a string by changing case

Print all permutations of a string keeping the sequence but changing cases.

Examples:

```Input : ab
Output : AB Ab ab aB

Input : ABC
Output : abc Abc aBc ABc abC AbC aBC ABC
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1 (Naive) : Naive approach would be to traverse the whole string and for every character, consider two cases, (1) change case and recur (2) Do not change case and recur.

Method 2 (Better) For a string of length n there exists 2n maximum combinations. We can represent this as a bitwise operation.
The same idea is discussed in Print all subsequences.

Below is the implementation of above idea :

## C++

 `// CPP code to print all permutations ` `// with respect to cases ` `#include ` `using` `namespace` `std; ` ` `  `// Function to generate permutations ` `void` `permute(string input) ` `{ ` `    ``int` `n = input.length(); ` ` `  `    ``// Number of permutations is 2^n ` `    ``int` `max = 1 << n; ` ` `  `    ``// Converting string to lower case ` `        ``transform(input.begin(), input.end(), input.begin(),  ` `                                                ``::``tolower``); ` `    ``// Using all subsequences and permuting them ` `    ``for` `(``int` `i = 0; i < max; i++) { ` `         `  `        ``// If j-th bit is set, we convert it to upper case ` `        ``string combination = input; ` `        ``for` `(``int` `j = 0; j < n; j++)  ` `            ``if` `(((i >> j) & 1) == 1) ` `                ``combination[j] = ``toupper``(input.at(j));      ` ` `  `        ``// Printing current combination ` `        ``cout << combination << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``permute(``"ABC"``); ` `    ``return` `0; ` `} `

## Java

 `// Java program to print all permutations ` `// with respect to cases ` ` `  `public` `class` `PermuteString  ` `{ ` `    ``// Function to generate permutations ` `    ``static` `void` `permute(String input) ` `    ``{ ` `        ``int` `n = input.length(); ` `         `  `        ``// Number of permutations is 2^n ` `        ``int` `max = ``1` `<< n; ` `         `  `        ``// Converting string to lower case ` `        ``input = input.toLowerCase(); ` `         `  `        ``// Using all subsequences and permuting them ` `        ``for``(``int` `i = ``0``;i < max; i++) ` `        ``{ ` `            ``char` `combination[] = input.toCharArray(); ` `             `  `            ``// If j-th bit is set, we convert it to upper case ` `            ``for``(``int` `j = ``0``; j < n; j++) ` `            ``{ ` `                ``if``(((i >> j) &  ``1``) == ``1``) ` `                    ``combination[j] = (``char``) (combination[j]-``32``); ` `            ``} ` `             `  `            ``// Printing current combination ` `            ``System.out.print(combination); ` `            ``System.out.print(``"   "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver Program to test above function ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``permute(``"ABC"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Sumit Ghosh `

## Python

 `# Python code to print all permutations ` `# with respect to cases ` ` `  `# Function to generate permutations ` `def` `permute(inp): ` `    ``n ``=` `len``(inp) ` `  `  `    ``# Number of permutations is 2^n ` `    ``mx ``=` `1` `<< n ` `  `  `    ``# Converting string to lower case ` `    ``inp ``=` `inp.lower() ` `     `  `    ``# Using all subsequences and permuting them ` `    ``for` `i ``in` `range``(mx): ` `        ``# If j-th bit is set, we convert it to upper case ` `        ``combination ``=` `[k ``for` `k ``in` `inp] ` `        ``for` `j ``in` `range``(n): ` `            ``if` `(((i >> j) & ``1``) ``=``=` `1``): ` `                ``combination[j] ``=` `inp[j].upper() ` `  `  `        ``temp ``=` `"" ` `        ``# Printing current combination ` `        ``for` `i ``in` `combination: ` `            ``temp ``+``=` `i ` `        ``print` `temp,  ` `         `  `# Driver code ` `permute(``"ABC"``) ` ` `  `# This code is contributed by Sachin Bisht `

## C#

 `// C# program to print all permutations ` `// with respect to cases ` `using` `System; ` ` `  `class` `PermuteString  { ` `     `  `    ``// Function to generate  ` `    ``// permutations ` `    ``static` `void` `permute(String input) ` `    ``{ ` `        ``int` `n = input.Length; ` `         `  `        ``// Number of permutations is 2^n ` `        ``int` `max = 1 << n; ` `         `  `        ``// Converting string ` `        ``// to lower case ` `        ``input = input.ToLower(); ` `         `  `        ``// Using all subsequences  ` `        ``// and permuting them ` `        ``for``(``int` `i = 0;i < max; i++) ` `        ``{ ` `            ``char` `[]combination = input.ToCharArray(); ` `             `  `            ``// If j-th bit is set, we  ` `            ``// convert it to upper case ` `            ``for``(``int` `j = 0; j < n; j++) ` `            ``{ ` `                ``if``(((i >> j) & 1) == 1) ` `                    ``combination[j] = (``char``) (combination[j] - 32); ` `            ``} ` `             `  `            ``// Printing current combination ` `            ``Console.Write(combination); ` `            ``Console.Write(``" "``); ` `        ``} ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `Main()  ` `    ``{ ` `        ``permute(``"ABC"``); ` `    ``} ` `} ` ` `  `// This code is contributed by Nitin Mittal. `

## PHP

 `> ``\$j``) & 1) == 1) ` `                ``\$combination``[``\$j``] = ``chr``(ord(``\$combination``[``\$j``]) - 32); ` `        ``} ` `         `  `        ``// Printing current combination ` `        ``echo` `\$combination` `. ``" "``; ` `    ``} ` `} ` ` `  `// Driver Code ` `permute(``"ABC"``); ` ` `  `// This code is contributed by mits ` `?> `

Output:

```abc Abc aBc ABc abC AbC aBC ABC
```

This article is contributed by Rohit Thapliyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.