#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>

char letters[] = {'a', 'b', 'c'};

template <typename T, std::size_t N>
std::size_t array_size(T (&)[N])
{ return N; }

bool next(std::string& in)
{
    for(std::size_t i = 0; i < in.size(); ++i)
        if(in[i] == letters[array_size(letters) - 1]){ //if current letter is last in set ...
            in[i] = letters[0]; //Then we assign first letter and let loop continue
        } else {
            std::size_t pos = std::distance(std::begin(letters),
                                std::find(std::begin(letters), std::end(letters), in[i]));
            in[i] = letters[++pos]; //Else we assign next letter in set and return true
            return true;            //As indication that it is not the last possible string
        }
    return false;      //If loop finished, we exhausted all strings
}

void print_all()
{
    constexpr std::size_t max = 4;
    for(std::size_t size = 1; size <= max; ++size) {//Generate sequences of all possible length
        //Create string of correspomding length. Note that we CANNOT use space as initializer
        //Or nothing would work at all.
        std::string guess(size, letters[0]);
        bool again = false;
        do { //Inner loop, generates all possible variation of given length
            again = next(guess); //Generate next string
            std::cout << guess << '\n';
        } while( again );
        std::cout << '\n';
    }
}

int main ()
{
    print_all();
}
