Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge #9002

Name: Anonymous 2011-05-02 20:12

The task:

Print strings from "a" to "zzzzz" without using any loop or conditional statements. Don't just write all 1000 permutations out by hand. The output should look like this:
a
b
c
...
aa
ab
ac
...
zzzzx
zzzzy
zzzzz


inb4 lipthfags and dead dogs using some obscure functionality of their obscure languages.

Name: Anonymous 2012-01-15 7:33

Learn with me...


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

void display_word(unsigned long long word, int n)
{
  char *buffer = malloc (n+1);       // Allocates enough space for word in buffer
  buffer[n] = '\0';                  // Make string end with \0
  while (n)
  {
    buffer[--n] = word % 26 + 'A';
    word /= 26;
  }
  printf ("%s\n", buffer);           // Displays the word
  free (buffer);
}

int main ()
{
  int length = 1;
  unsigned long long word = 0;
  unsigned long long limit = 26;
  while (1)
  {
    display_word (word++, length);

    if (word == limit)
    {
      word = 0;
      length++;
      limit *= 26;
    }
  }
}


Do I win?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List