Search
 
SCRIPT & CODE EXAMPLE
 

CPP

all permutations with repetition C++

#include <string>
#include <iostream>


void print_str(const char*,std::string,const int, const int);

int main()

{

    int lenght = 2;

    char str[] = {'A', 'B', 'C', 'D'};



    int n = sizeof str;

    print_str(str, "", n, lenght);  //Note: this function works on all cases and not just the case above

    return 0;

}

// The main recursive method to print all possible strings of length "length"
    void print_str(const char str[],std::string prefix,const int n, const int lenght)

    {

        if (lenght == 1)

            {

                for (int j = 0; j < n; j++)

                std::cout << prefix + str[j] << std::endl;

            }//Base case: lenght = 1, print the string "lenght" times + the remaining letter

        else

            {


               // One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
                for (int i = 0; i < n; i++)

                // Next character of input added
                print_str(str, prefix + str[i], n, lenght - 1);
                // "lenght" is decreased, because we have added a new character

            }

    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ header boilerplate 
Cpp :: how to say hello world in c++ 
Cpp :: c++ find object in vector by attribute 
Cpp :: c++ split string by space into array 
Cpp :: comparing characters of a string in c++ 
Cpp :: how to make window resizable in sdl 
Cpp :: cpp map insert 
Cpp :: c++ range based for loop 
Cpp :: c++ get pointer from unique_ptr 
Cpp :: string format decimal places c++ 
Cpp :: system("pause") note working c++ 
Cpp :: define vector with size and value c++ 
Cpp :: sum array c++ 
Cpp :: constructor syntax in c++ 
Cpp :: preorder 
Cpp :: install qpid broker in ubuntu/linux 
Cpp :: trie code cpp 
Cpp :: C++ program for Celsius to Fahrenheit and Fahrenheit to Celsius conversion using class 
Cpp :: UENUM ue4 
Cpp :: if argv == string 
Cpp :: put function in cpp 
Cpp :: stoi in c++ 
Cpp :: cin does not wait for input 
Cpp :: c++ for loop syntax 
Cpp :: loops in c and c ++ 
Cpp :: C++ float and double Using setprecision() For Floating-Point Numbers 
Cpp :: c++ else if 
Cpp :: nth fibonacci number 
Cpp :: c++ itoa 
Cpp :: create a copy of a vector c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =