1. ## C Counting Algorithm

Hi,

I've written the code below which prints every possible combination of the lowercase alphabet for three letters. I'm wondering if anybody knows how I can change this code so it can work with a variable length array? I've been trying to work this out for a while now, it seems pretty tricky.

Thanks.

Code:
```#include <stdio.h>

static const char *s = "abcdefghijklmnopqrstuvwxyz";

static void printarr(const char *arr[], int len)
{
int i;

for(i = 0; i < len; i++)
printf("%c", *arr[i]);
printf("\n");
}

int main()
{
const char *arr[3];

for(arr[0] = s; *arr[0] != '\0'; arr[0]++) {
for(arr[1] = s; *arr[1] != '\0'; arr[1]++) {
for(arr[2] = s; *arr[2] != '\0'; arr[2]++) {
printarr(arr, 3);
}
}
}
return 0;
}```

2. In the same way that adding one to
"000009"
gets you to
"000010"

You could also do
"aaaaaz"
becomes
"aaaaba"

Just prepare your char array with as many "a"'s as you need, then count up to 'z' at the right hand side, working any "carry" to the left.

3. This is all you need for a VLA:
Code:
```    int len = 5;
const char *arr[len];```
Now, in order to actually process that, since you can't dynamically change the number of nested loops in your code, you're going to have to make some sort of recursive function that prints everything out.

4. A recursive function is no better than looping. It would have the same problems.

Salem's solution works.

5. Originally Posted by King Mir
A recursive function is no better than looping. It would have the same problems.

Salem's solution works.
Yeah, you're right. Don't know what I was thinking...