It finds the anagram of a word from a set of pre processed words....
#include
#include
#include
class finder
{
private:
char words[1300][10],input[30][10],output[110];
int fileCounter,flag;
long unsigned int wordPro[1300],outputProduct[10];
int letterCounter,inputCounter,outputCounter,prime;
char c;
FILE *dictonary;
public:
finder()
{
prime=37;
fileCounter=letterCounter=flag=inputCounter=outputCounter=0;
for (int i=0;i<1300;i++)
wordPro[i]=37;//assign it to any prime coz the product
//of any set of numbers is always
//unique.
for(int i=0;i<10;i++)
outputProduct[i]=37;
}
void fillArray();
void getInput();
void getProduct();
void getOutputWithPrime();
};
int main() {
finder f1;
f1.fillArray();
f1.getProduct();
f1.getInput();
f1.getOutputWithPrime();
return 0;
}
void finder::fillArray()
{
dictonary = fopen("dump1","r");//file which stores the words..
//each word in a new line
do {
c = fgetc (dictonary);
if ((c>=48 && c<=59)||(c>=97 && c<=122)||c==41)//characters that I wanted
//to read-in case
//the list contained some unwanted characters.
{
words[fileCounter][letterCounter++]=c;
flag=0;
}
if((c==10||c==13)&&flag==0)
{
words[fileCounter++][letterCounter]='\0';
letterCounter=0;
flag=1;
}
} while (c != EOF);
fclose(dictonary);
}
void finder::getOutputWithPrime()
{
for (int i=0;i
for (int j=0;j
continue;
else
{ printf("%s,",words[j]);
strcpy(output,words[j]);strcpy(output,",");goto level1;
}
}
level1:;
}
printf("\n");
}
void finder::getProduct()
{
for(int i=0;i
for (int j=0;j
}
}
void finder::getInput()
{
char temp[30];
scanf("%s",temp);
while(strcmp(temp,"end")!=0)
{
if(!strcmp(temp,"#")==0)
{
strcpy(input[inputCounter],temp);
for (int i=0;i
inputCounter++;
}
scanf("%s",temp);
}
}
Thursday, September 18, 2008
Simplest Anagram Finder.
Posted by craxfun at 8:17 AM 0 comments
Labels: anagram, level 1, programming challenges
Subscribe to:
Posts (Atom)