#include<stdio.h>
#include<string.h>
void FindWord (char used[30] , char string[30] , int wordsize);
char* CleanString (char string[30], int wordsize);
int main (void)
{
FILE *fp;
char letters[30];
char words[30];
char used[30];
char *string;
int wordsize;
int input_size;
input_size = wordsize = 0;
fp = fopen("dictionary.txt","r");/*contains a list of words in the following format : number_of_chars.word (i.e. 2.hi , 4.stop )*/
while(1)
{
printf("Type the letters and press enter : ");
gets(letters);/* saves the inputed data to a string*/
fflush(stdin);
input_size = strlen(letters);/* gets how many characters user entered*/
while (1)
{
fscanf(fp,"%d",&wordsize);/* will read the number (i.e. for 2.hi will read 2) to determine the size of the word*/
string = fgets(words, 30 , fp);
strcpy(used, letters);/*copies the inputed string into a new array , go to FindWord () for details*/
if (string == NULL) /*if the file ends or we have an error, break*/
break;
if (input_size != wordsize) /*if the number of letters we inputed is different from the size of the word in question , skip it*/
continue;
CleanString (string, wordsize);/*This function removes the number_of_letters. from the string "string" .*/
FindWord (used, string, wordsize);/* this function checks if the letters inputed can make a word and if so , prints it*/
}
}
fclose(fp);
}
void FindWord (char used[30] , char string[30] , int wordsize)
{
int match = 0;
for ( int i = 0; i<=wordsize; i++)
{
for ( int j = 0; j<=wordsize; j++)
{
if (used[j] == string[i])/*the used[] array is just a copy of the inputed string*/
{
match++;/*inputed character matches the one of the word*/
used[j] = 0;/*replaces the matched character with 0 so we don't end up using it again (better way to do this?)*/
if (match == wordsize )/*if we matched as many characters as the characters in the word then print it */
printf("%s",string);
}
}
}
}
char* CleanString (char string[30], int wordsize)
{
for (int i = 0; i<wordsize; i++)
{
if (string[i] == '.')
{
for (int j = 0; j <= wordsize; j++)
string[j] = string[(i+1)+j];
}
}
return string;
}
Questions :
1) How can i make it work with greek words? (my windows are in english)
if i use a dictionary with greek words it shows them as blank space.
2) Why my first while works only once?
3) Why if i enter a printf under strcpy(used, letters); it takes ALOT longer to compute?
Thank you for your time ,
HadoukenGr