mardi 20 octobre 2020

Generate all possible strings [closed]

UPDATED:

I'm new to C++ and failed writing the following code after multiple hours. I want to write a code that generates all combination of words using the following characters (and prints them) so that its length is k (integer given as parameter)

int alphabet[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','q','y','z'
        ,'0','1','2','3','4','5','6','7','8','9','_'};

Suggested solution using recursion:

bool generate(char alphabet[],int num_of_alphbet,char* word,int index, int full_lengh)
{
    if (index==full_lengh+1)
    {
        cout << word << endl;
    }

    for (int i=0;i<num_of_alphbet;i++)
    {
        char tmp=alphabet[i];
        word[index]=alphabet[i];
        generate(alphabet,num_of_alphbet,word,index+1,full_lengh);
        word[index]=tmp;
    }
}

Aucun commentaire:

Enregistrer un commentaire