Bogusław Kempny

strfry()

×
Autor adres Polski
About HC-SR04 LCD Camera fork() Short Message Service strfry() GPIO pulses Keypad Gate GPIO PWM SG90 RFID Graphologist Time and attendance Shutdown Temperature ....











Now, let's have fun.

In the GNU C library, there is a function called strfry().

A few have heard of it, and fewer actually use it. What does it do? Well, it rearranges letters in a text. It creates an anagram.

The text "Humpty Dumpty" can be transformed into:

"umyptHumtDy p"
"uHtm pyDtupmy"
"yyupt tHDumpm"
" yupummtpyDtH"

It seems that we need to take this function "with a pinch of salt". Its creators probably treated it that way too and wrote it just for fun. It can only be found in the GNU C library.

Because for what, apart from word puzzles, can we use it for? Surely not for encryption!

And yet it can be handy. For a programmer, it can be great fun and also produce something useful.

Years ago, and just for fun, I wrote in C a small program called lotto.c. A simple one, only 38 lines of code, but it was using a two-dimensional array, pseudo-random number generator, sort. It took a bit of work to write and launch it.

Recently, also for fun, I wrote its new version using strfry().

It does the same, but has shrunk to 12 lines of code and the part responsible for the generation of the six lucky numbers, to one line:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
  unsigned char buf[50];
  buf[50] = 0x00;
  int i;

  for(i=0;i<49;i++)
    buf[i]=i+1;
  buf[49] = 0x00;
  strfry(buf);
  for(i=0;i<6;i++)
    printf("%2d  ", buf[i]);
    printf("\n");
}
So if you play lotto, copy the lotto.c program and then compile:

cc lotto.s -o lotto

Run:

./lotto

And lose!