#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

The program will ask the user to enter some letters and will try to find if that letters can make a word.
To do that it reads data from a file called dictionary.

example of dictionary.txt :
2.hi
4.post
4.wind
3.yes
As you can see we have the number of letters a word contains followed by a '.' and the word.
This is needed so that we only compare words with as many letters as the user typed.

For example , if user enters "opst" the output will be something like this :

post
stop
tops

It works with English (gets the job done) , but i want to know how can i do this with languages like Greek.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.