I want to build a list containing every possible permutation of capitalization of a word. so it would be
List permutate(string word)
{
List
If order doesn't matter, you can try Linq. We enumerate all binary numbers in a range of [0..2**word.Length]. and treat each number as mask: 0 - lower case, 1 - upper case. For instance for happy we have
mask string
----------------
00000 -> happy
00001 Happy
00010 hAppy
00011 HAppy
00100 haPpy
00101 HaPpy
...
11111 HAPPY
Code:
using System.Linq;
...
List permutate(string word) =>
Enumerable
.Range(0, 1 << word.Length)
.Select(mask => string.Concat(word.Select((c, i) => (mask & (1 << i)) == 0
? char.ToLower(c)
: char.ToUpper(c))))
.ToList();
Demo:
Console.Write(string.Join(", ", permutate("happy")));
Outcome:
happy, Happy, hAppy, HAppy, haPpy, HaPpy, hAPpy, HAPpy, hapPy, HapPy, hApPy, HApPy, haPPy, HaPPy, hAPPy, HAPPy, happY, HappY, hAppY, HAppY, haPpY, HaPpY, hAPpY, HAPpY, hapPY, HapPY, hApPY, HApPY, haPPY, HaPPY, hAPPY, HAPPY