I coded the solution in C (if you don't mind me posting C code in Java forum ^^):
Basically it sorts the scrambled word and then loops through every word in the dictionary, and if the word's length is the same as the scrambled word it copies the scrambled word and then sorts that copy. After that it tries to match the two sorted words and if they are the same then the scrambled is found.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 7
char scrambled_word[] = "esmdcrlab"; // scrambled
char *dictionary[SIZE] = {"world", "array", "testing", "largeword", "abitlargerword", "scrambled", "download"};
int compare(const void *a, const void *b)
{
return ( *(char*)a - *(char*)b );
}
int matchWord(const char *scrambled, char *word)
{
// assuming scrambled is aready sorted
size_t wlen = strlen(word);
char test_word[wlen+1];
if (strlen(scrambled) == wlen){
strncpy(test_word, word, wlen);
qsort(test_word, wlen, sizeof(char), compare);
if (!strncmp(test_word, scrambled, wlen))
return 1;
}
return 0;
}
int main(void)
{
size_t scrlen = strlen(scrambled_word);
char orig_scrambled[scrlen+1];
strncpy(orig_scrambled, scrambled_word, scrlen);
qsort(scrambled_word, scrlen, sizeof(char), compare);
int i;
for (i = 0; i < SIZE; i++){
printf("Testing: %s\n", dictionary[i]);
if (matchWord(scrambled_word, dictionary[i])){
printf("[+] %s is %s !\n", orig_scrambled, dictionary[i]);
return 0;
}
}
printf("[-] %s is not in the dictionary\n", scrambled_word);
return 0;
}