“Random Facebook quiz” (part 3/3)

[et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text”]

Well I can’t leave this alone. Here’s a ‘C’ version:


#include
#include
#include
char *words[] = {
"tsar", "rat", "tar", "star",
"tars", "cheese", "mow", "lox",
NULL
};
#define LETTERS 26
// the following assumes the alphabet is no more than 256 symbols
static char hashcount[LETTERS];
// the following assumes a word's symbols never repeats more than 256
static char hashbuffer[LETTERS*3+1];
char *hash(char *word) {
char *ptr = word;
int i;
for (i=0; i= 'a' && c <= 'z') hashcount[c-'a']++; ptr++; } ptr = hashbuffer; for (i=0; i 1) {
sprintf(ptr, "%x", hashcount[i]);
ptr += (hashcount[i]/16 + 1);
}
}
}
*ptr = '\0';
return strdup(hashbuffer);
}
int main(int argc, char *argv[]) {
char **id = NULL;
char *printed = NULL;
int i, j, len=0;
while (1) {
if (words[len] == NULL) break;
len++;
}
id = (char **)malloc(len*sizeof(char *));
printed = (char *)malloc(len*sizeof(char));
for (i=0; i

...and its output:


[0] tsar : arst
[1] rat : art
[2] tar : art
[3] star : arst
[4] tars : arst
[5] cheese : ce3hs
[6] mow : mow
[7] lox : lox
===
tsar
star
tars
===
rat
tar
===
cheese
===
mow
===
lox

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.