Algorithm for Character Generation

Joined
Jul 17, 2007
Messages
532
Reaction score
0
I've been racking my brains for about an hour now trying to figure out how to Generate logical character progression (a to Z, aa to ZZ, aaa to ZZZ etc)

And so far can't for the life of me work out how to do it.

I figured the easiest way would be to have an array holding all allowed characters.


I just can't for the life of me figure out any type of 'algorithm' to do it.

Any help? :tongue: Its annoying me.

~ Jeax
 
Urm, you want to check if a certain text string has valid characters?

Well, I don't know what language you use, but I think this is uni (works in C/C++):

You iterate through the string, each character you check it's integer value. You check if that value is between the valid bounds.

a = 97
z = 122
A = 65
Z = 90
0 = 48
9 = 57

So, to check if its a-zA-Z, you do this:

if ((string > 64 && string < 91) || (string > 96 && string < 123))
{
...
}

Hope this helps, not quite sure what you mean with an algorithm...
 
Recursion.
Have a string of length MAX_LENGTH, formatted to zeros (or anything else) and to proceed to the next combination, just call the function.
Code:
void rollNext(char str[length], int max, int pos)
{
  str[pos]++;
  if (str[pos] < 'A')
  {
    str[pos] = 'A';
    return;
  }
  if (str[pos] > 'Z' && str[pos] < 'a')
  {
    str[pos] = 'a';
    return;
  }
  if (str[pos] > 'z')
  {
    if (pos == 0)
    {
      return;
    }
    str[pos] = 'A';
    rollNext(str, MAX_LENGTH, pos - 1);
  }
  return;
}
Not sure if I completely got what you meant, either...
 
Recursion.
Have a string of length MAX_LENGTH, formatted to zeros (or anything else) and to proceed to the next combination, just call the function.

Why do you use recursion while a 'while'/'for' loop would be more logical?

This will call the function over and over again, everytime a function is called the parameters are copied in the stack, as well as the return value. Many function calls should be avoided. Easiest would be a for loop.

Btw, I forgot you could simply do 'a' instead of 97 ^^
 
Why do you use recursion while a 'while'/'for' loop would be more logical?

This will call the function over and over again, everytime a function is called the parameters are copied in the stack, as well as the return value. Many function calls should be avoided. Easiest would be a for loop.
Cause I like it and to me it seems easier here. It's not that bad with just 2 (define max length globally) parameters and no return value, anyway.

Besides, it's only every 52nd time the function will even have to be called again so it's not significantly slower, if at all: (2 pushes to stack,) 1 jump, a few to 10 commands, return, (2 pops), while a loop structure compiles to a few additional commands, aswell.
 
Back