#include <stdio.h>
const char words[][10] = {"shake", "face", "eye", "work", "length", "head", "fly"};
const int lengths[] = {5, 4, 3, 4, 6, 4, 3};
const int max = sizeof(lengths) / sizeof(int);
int positions[] = {0, 0, 0, 0, 0, 0, 0};
FILE *grepFile = NULL;

/** Increment the counters, return 1 when overflow */
int increment() {
    int maxCounter = 0;
    for (int i=0; i<max; i++) {
        if (positions[i] == lengths[i] - 1)
            maxCounter++;
    }
    if (maxCounter == max)
        return 1;
    positions[0]++;
    for (int i=0; i<max; i++) {
        if (positions[i] == lengths[i]) {
            positions[i] = 0;
            positions[i+1]++;
        }
    }
    return 0;
}

int printCounters() {
    for (int i=0; i<max; i++)
        printf("%d ", positions[i]);
}

int printLetters() {
    for (int i=0; i<max; i++) {
        printf("%c", words[i][positions[i]]);
        if (grepFile)
            fprintf(grepFile, "%c", words[i][positions[i]]);
    }
    if (grepFile)
        fprintf(grepFile, "\n");
}

int main(int argc, char **argv) {
    grepFile = fopen("inputwords.txt", "w");
    do {
        printCounters();
        printLetters();
        printf("\n");
    }while(increment() == 0);
    fclose(grepFile);
    return 0;
}
