[Solved] how do I generate set of all tokens of length exactly equal to 8


You caught me on a day where I don’t want to do what I’m supposed to be doing1.

The code below only generates token output; it doesn’t attempt to store tokens anywhere. You can redirect the output to a file, but as others have pointed out, you’re going to need a bigger boat hard drive to store 368 strings. There’s probably a way to do this without the nested loops, but this method is pretty straightforward. The first loop updates the counter in each position, while the second maps the counter to a symbol and writes the symbol to standard output.

You can set LEN to a smaller value like 3 to verify that the program does what you want without generating terabytes of output. Alternately, you could use a smaller set of characters for your digits. Ideally, both LEN and digs should be command-line parameters rather than constants, but I’ve already spent too much time on this.

Edit

Okay, I lied. Apparently I haven’t spent too much time on this, because I’ve cleaned up a minor bug (the first string isn’t displayed correctly because I was doing the update before the display) and I’ve made the length and character set command-line inputs.

Note that this code assumes C99.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define DEFAULT_LEN 8

int main( int argc, char **argv )
{
  const char *default_digs="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  size_t len = DEFAULT_LEN;
  const char *digs = default_digs;

  if ( argc > 2 )
    digs = argv[2];

  if ( argc > 1 )
    len = strtoul( argv[1], NULL, 10 );

  int idx[len];
  memset( idx, 0, sizeof idx );

  size_t diglen = strlen( digs );

  for(;;)
  {
    int j = len;
    while( j )
      putchar( digs[idx[--j]] );

    putchar( '\n' );

    while ( j < len && idx[j] == diglen - 1 )
      idx[j++] = 0;

    if ( j == len )
      break;

    idx[j]++;
  }

  return 0;
}

Sample output:

[fbgo448@n9dvap997]~/prototypes/tokgen: ./tokgen 2 01
00
01
10
11

[fbgo448@n9dvap997]~/prototypes/tokgen: ./tokgen 3 01
000
001
010
011
100
101
110
111

[fbgo448@n9dvap997]~/prototypes/tokgen: ./tokgen 2 012
00
01
02
10
11
12
20
21
22


1. Which, to be fair, is pretty much any day ending in a ‘y’.

solved how do I generate set of all tokens of length exactly equal to 8