String permutation using iteration and a filter I'm looking to improve my skills as a developer so I recently challenged myself to design a string permutation generator without recursion. To add to the algorithm I also decided that the string should have a filter to it. An example of a filter would be "lld" where "l" is any character a-z and "d" is any digit 0-9. The filter "lld" should produce all strings aa0-zz9. Would love feedback on how to improve this algo. Thanks.

Implementation in C++

#include &lt;iostream&gt;

#define CHAR_SET_LEN 26
#define INT_SET_LEN 10
#define FILTER_LEN 3

int main() {
    std::cout &lt;&lt; "Hello, World!" &lt;&lt; std::endl;

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

    bool doLoop = true;

    int filterStack[100];

    int filterLength = FILTER_LEN;

    char filter[FILTER_LEN+1] = "lld";

    for (int i=0; i &lt; filterLength; i++) {
        filterStack[i] = 0;
    }

    while (doLoop) {
        char string[FILTER_LEN];

        for (int a = 0; a &lt; filterLength; a++) {
            if (filter[a] == 'l') {
                string[a] = char_set[filterStack[a]];
            }
            else {
                string[a] = int_set[filterStack[a]];
            }

        }
//
        std::cout &lt;&lt; string &lt;&lt; std::endl;
//
        for (int z = (filterLength-1); z &gt; -1; z--) {
            filterStack[z] = filterStack[z] + 1;

            int finalFilter = INT_SET_LEN;
            if (filter[z] == 'l') {
                finalFilter = CHAR_SET_LEN;
            }

            if (filterStack[z] &lt; finalFilter) {
                break;
            }
            else {
                if (z == 0 &amp;&amp; (filterStack[z] == finalFilter)) {
                    doLoop = false;
                    break;
                }

                filterStack[z] = 0;
            }
        }
    }


    return 0;
}
Are you sure this is C++?

...because this is almost 100% C. Let's go over some things:


#define is discouraged in modern C++, as is reliance on the C preprocessor in general. Just define your constants with const and (depending on the situation) constexpr.
Don't define raw arrays. Use std::array for fixed size arrays, std::vector for dynamic arrays, or any other standard container class.
Don't use char arrays if you mean strings. C++ offers std::string for general string handling and std::stringstream for string building.
Use standard algorithms. For example, your first for-loop could be replaced with a call to std::fill.


That said, there are

Other Issues


Don't use std::endl. '\n' does the same thing, but doesn't include the unnecessary flush std::endl comes with.
You don't need char_set and int_set. You can generate all lowercase letter by adding their offset in the alphabet to 'a' (i.e. 'a' + 1 == 'b') thanks to the (almost) ubiquitous ASCII encoding. The same is true for digits.
int is not the right type for index variables. Use somethings more appropriate, such as std::size_t, instead.
You should export some of your code to proper functions. Just putting everything into main makes your code hard to read and prone to bugs.
As Cris Luengo pointed out in the comments already, string is not null-terminated, and thus you're causing undefined behavior when writing it to std::cout.
filter is one character longer than it needs to be. Since you never do something with it that would require it to be null terminated (and don't even null terminate the string, just keep an unused character at the end), it suffices to make its length FILTER_LEN.