Every permutation of the alphabet up to 29 characters?

后端 未结 13 1343
攒了一身酷
攒了一身酷 2021-02-11 01:23

I\'m attempting to write a program that will generate a text file with every possible permutation of the alphabet from one character up to twenty-nine characters. I\'ve chosen 2

13条回答
  •  佛祖请我去吃肉
    2021-02-11 02:16

    This is what I would do:

    #include 
    
    void printWords(std::string& word, int index, int last)
    {
        std::cout << word << "\n";
        if (index != last)
        {
            for(char loop = 'a'; loop <= 'z'; ++loop)
            {
                word[index] = loop;
                printWords(word, index+1, last);
            }
            word[index] = ' ';
        }
    }
    
    int main()
    {
        std::string word("                             "); // 29 space
    
        printWords(word,0,word.length());
    }
    

提交回复
热议问题