Alphabedecimal Revisited – Solution

The challenge for this month’s Exercise is to count in “alphabedecimal,” generating output from AAAA through ZZZZ, without using nested loops or a complex if structure. Your solution must use a recursive function that handles flipping the digits, er, characters.

This new approach to the old alphabedecimal puzzle stemmed from a project where I try to brute-force guess passwords. It’s an interesting puzzle, one that appears in an upcoming book. The attempt made me aware of a cleaner, better way to handle flipping digits in alphabetic output.

For my solution, I created the recursive function, flip():

int flip(char *c,int pos);

The function requires two arguments: a string to manipulate (char *c) and a character position to flip(int pos). The position argument is the key to eliminating the cascading if statements from the earlier solution.

The flip function calls itself when a character at the given position flips from Z to A. The position indicator is incremented and the next character in the string is incremented by calling the flip() function again.

The flip() function returns values 1 (TRUE) or 0 (FALSE) depending on whether the final character has flipped. When the final character is finally flipped, the function returns 0 (FALSE). This value falls through all the recursive calls to end the process.

Here is the full code for my solution:

2021_10-Exercise.c

#include <stdio.h>

int flip(char *c,int pos)
{
    (*(c+pos))++;                   /* increment the letter */
    if( *(c+pos) > 'Z' )            /* check for overflow */
    {
        *(c+pos) = 'A';             /* reset back to a */
        if( pos!=0 )                /* watch for overflow */
        {
            if( !flip(c,pos-1) )    /* flip the next column's digit */
                return(0);          /* echo down the FALSE return */
        }
        else
            return(0);              /* last column, FALSE return */
    }
    return(1);                      /* keep looping */
}

int main()
{
    const unsigned length = 4;
    char digits[length] = "AAAA";

    do
    {
        puts(digits);
    }
    while( flip(digits,length-1) );

    return(0);
}

At Line 5, The flip() function increments the character at position pos. If this character is greater than 'Z', it’s reset back to 'A'. The pos variable is compared with zero, which indicates the last character in the string. If pos isn’t zero, the flip() function is recursively called, incrementing the next digit in the string.

Because the flip() function does all the work, the main() function requires only a do-while loop to process the string. A puts() statement outputs the characters in digits[] as they turn from AAAA through ZZZZ.

I hope you devised a similar solution, using a recursive function to process characters in a string in a sequential manner. A recursive function eliminates not only nested for loops but a complex, cascading if decision tree.

By the way, I compared the timing for this solution with my original Exercise solution. Both run at about the same speed. I saw no advantage to making the function recursive versus the cascading if structure thing. Yet, I still prefer this solution to the problem.

Leave a Reply