Code:
/* finds jumbled words and matches them to a list of words, FAST!
REALLY fast!! :)
Adak, Jan 7, 2011
status: OK
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int i,j, k, n;
time_t start, stop;
int wrdNum[26]={0};
int libNum[26]={0};
char *lib[] = {"ability", "absence", "actions", "amazing", "believe", "browser",
"caption", "captive", "ceiling", "degrees", "denizen", "develop", "examine",
"example", "exploit", "fifteen", "focused", "fortune", "forward", "garbage",
"gasping", "graphic", "handgun", "hastily", "helpful", "iceberg", "impeach",
"inspect", "jawbone", "justify", "keyword", "kickoff", "kneepad", "lactose",
"latency", "legible", "madness", "magical", "manhunt", "mission", "nametag",
"nitrate", "nowhere", "officer", "optical", "outlook", "oxidize", "paradox",
"pathway", "patient", "payment", "qualify", "quality", "quarrel", "radical",
"railing", "reduced", "resolve", "savings", "sayings", "scissor", "shadows",
"tactics", "teacher", "terrify", "tractor", "unarmed", "unmasks", "updates",
"vaccine", "valleys", "walnuts", "wealthy", "whacked", "willing", "wizards",
"xysters", "yielded", "yoghurt", "younger", "zippers", "zombies"
};
char jumbled[8], c;
time_t t;
printf("\n\n\n");
//choose a word at random
srand((unsigned) time(&t));
n=(rand() % 82);
//random word goes into "jumbled[]"
strcpy(jumbled, lib[n]);
for(i=0;i<7;i++) { //swap random letters around to jumble the word
j=rand()%7;
c=jumbled[i];
jumbled[i]=jumbled[j];
jumbled[j]=c;
}
jumbled[7]='\0';
printf("\nJumbled word is: %s Library word is: %s", jumbled, lib[n]);
start=clock(); //start the clock!
for(i=0;i<7;i++) { //build the distribution of letters array
wrdNum[jumbled[i]-'a']++; //wrdNum[0]=number of a's in the word, etc
} //for the jumbled word
for(i=0;i<82;i++) { //build an equal array, with each word
for(j=0;j<7;j++) { //in the library
libNum[lib[i][j]-'a']++;
}
k=0;
while(libNum[k]==wrdNum[k] && k<26) //while the letter distributions
++k; //match, increment k
if(k<26) { //not a match
for(n=0;n<26;n++) //reset the libNum[] array to all zero's
libNum[n]=0;
}else //we have a match
break;
}
stop=clock();
printf("\n\nElapsed seconds: %f", (stop-start)/18.2); //same as CLK_TCK, etc
printf("\nJumbled word matches %s", lib[i]);
printf("\n\n\t\t\t press enter when ready");
(void) getchar();
return 0;
}