Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- void FindWord (char letters[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]={0};
- char *string;
- int wordsize;
- int input_size;
- fp = fopen("dictionary.txt","r");/*contains a list of words in the following format : number_of_chars.word (i.e. 2.hi , 4.stop )*/
- if (fp == NULL) {printf("\nError : dictionary.txt , no such file or directory!");}
- while(fp != NULL)
- {
- rewind(fp);
- 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);
- 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 (letters, string, wordsize);/* this function checks if the letters inputed can make a word and if so , prints it*/
- }
- }
- fclose(fp);
- }
- void FindWord (char letters[30] , char string[30] , int wordsize)
- {
- int not_matched,not_matched_rev;
- bool wordfound = true;
- not_matched = not_matched_rev = 0;
- for ( int i = 0; i<wordsize; i++)
- {
- not_matched = 0;
- not_matched_rev= 0;
- for ( int j = 0; j<wordsize; j++)
- {
- if (letters[i] != string[j])
- not_matched++;
- if (letters[j] != string[i])
- not_matched_rev++;
- if (not_matched == wordsize || not_matched_rev == wordsize)
- {
- i = wordsize + 1;
- wordfound = false ;
- }
- }
- }
- if(wordfound)
- {
- printf("\t->%s\n",string);
- fflush(stdout);
- }
- }
- char* CleanString (char string[30], int wordsize)
- {
- for (int i = 0; i<=2; i++)
- {
- if (string[i] == '.')
- {
- for (int j = 0; j <= wordsize; j++)
- string[j] = string[(i+1)+j];
- }
- }
- string[wordsize] = '\0';
- return string;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement