There are many different ways. The classic way is to establish the correspondence between your string and the number in the n -thick notation (for your case, n == 3 ). I will show a simpler from the ideological point of view option that simulates the transition to the next line. For convenience, I will use character arrays.
class Alphabet { int size; List<char> letters; Dictionary<char, int> indices; public Alphabet(IEnumerable<char> letters) { this.letters = letters.ToList(); this.indices = this.letters.Select((c, idx) => new { c, idx }) .ToDictionary(p => pc, p => p.idx); this.size = this.letters.Count; } public bool Advance(ref char c) { var letterPos = indices[c] + 1; if (letterPos == size) letterPos = 0; c = letters[letterPos]; return letterPos > 0; } public bool Advance(char[] chars) { for (int index = chars.Length - 1; index >= 0; index--) { if (Advance(ref chars[index])) return true; } return false; } }
Having such an auxiliary class, you can do different things. For example, the output of all lines:
var abc = new Alphabet("abc"); var chars = "aaa".ToCharArray(); do { Console.WriteLine(new string(chars)); } while (abc.Advance(chars));
The output lines from "bac" to "cab" :
var chars2 = "bac".ToCharArray(); do { var s = new string(chars2); Console.WriteLine(s); if (s == "cab") break; } while (abc.Advance(chars2));
etc.
cda cda cda? o_O - Qwertiy ♦