Iterating through the Alphabet - C# a-caz

后端 未结 10 1102
無奈伤痛
無奈伤痛 2020-11-29 03:03

I have a question about iterate through the Alphabet. I would like to have a loop that begins with \"a\" and ends with \"z\". After that, the loop begins \"aa\" and count to

10条回答
  •  孤街浪徒
    2020-11-29 03:35

    Here's my attempt using recursion:

    public static void PrintAlphabet(string alphabet, string prefix)
    {
        for (int i = 0; i < alphabet.Length; i++) {
            Console.WriteLine(prefix + alphabet[i].ToString());
        }
    
        if (prefix.Length < alphabet.Length - 1) {
            for (int i = 0; i < alphabet.Length; i++) {
                PrintAlphabet(alphabet, prefix + alphabet[i]);
            }
        }
    }
    

    Then simply call PrintAlphabet("abcd", "");

提交回复
热议问题