Advertisement
HadoukenGr

Find the Word v4

Jan 19th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4.  
  5. void FindWord (char letters[30] , char string[30] , int wordsize);
  6. char* CleanString (char string[30], int wordsize);
  7.  
  8. int main (void)
  9. {
  10.     FILE *fp;
  11.     char letters[30];
  12.     char words[30];
  13.     char used[30]={0};
  14.     char *string;
  15.     int wordsize;
  16.     int input_size;
  17.  
  18.  
  19.     fp = fopen("dictionary.txt","r");/*contains a list of words in the following format : number_of_chars.word (i.e. 2.hi , 4.stop )*/
  20.     if (fp == NULL) {printf("\nError : dictionary.txt , no such file or directory!");}
  21.  
  22. while(fp != NULL)
  23. {
  24.         rewind(fp);
  25.         printf("Type the letters and press enter : ");
  26.         gets(letters);/* saves the inputed data to a string*/
  27.         fflush(stdin);
  28.  
  29.         input_size = strlen(letters);/* gets how many characters user entered*/
  30.  
  31.  
  32.         while (1)
  33.         {
  34.             fscanf(fp,"%d",&wordsize);/* will read the number (i.e. for 2.hi will read 2) to determine the size of the word*/
  35.             string = fgets(words, 30 , fp);
  36.  
  37.  
  38.             if (string == NULL) /*if the file ends or we have an error, break*/
  39.                                     break;
  40.             if (input_size != wordsize) /*if the number of letters we inputed is different from the size of the word in question , skip it*/
  41.                                     continue;
  42.  
  43.  
  44.             CleanString (string, wordsize);/*This function removes the number_of_letters. from the string "string" .*/
  45.             FindWord (letters, string, wordsize);/* this function checks if the letters inputed can make a word and if so , prints it*/
  46.  
  47.         }
  48.  
  49. }
  50.     fclose(fp);
  51.  
  52. }
  53.  
  54. void FindWord (char letters[30] , char string[30] , int wordsize)
  55. {
  56.     int not_matched,not_matched_rev;
  57.     bool wordfound = true;
  58.     not_matched = not_matched_rev = 0;
  59.  
  60.             for ( int i = 0; i<wordsize; i++)
  61.             {
  62.                 not_matched = 0;
  63.                 not_matched_rev= 0;
  64.  
  65.                 for ( int j = 0; j<wordsize; j++)
  66.                 {
  67.                     if (letters[i] != string[j])
  68.                                         not_matched++;
  69.                     if (letters[j] != string[i])
  70.                                         not_matched_rev++;
  71.                     if (not_matched == wordsize || not_matched_rev ==  wordsize)
  72.                         {
  73.                             i = wordsize + 1;
  74.                             wordfound = false ;
  75.                         }
  76.  
  77.                 }
  78.             }
  79.  
  80.             if(wordfound)
  81.             {
  82.                 printf("\t->%s\n",string);
  83.                 fflush(stdout);
  84.             }
  85. }
  86.  
  87. char* CleanString (char string[30], int wordsize)
  88. {
  89.     for (int i = 0; i<=2; i++)
  90.     {
  91.         if (string[i] == '.')
  92.         {
  93.             for (int j = 0; j <= wordsize; j++)
  94.                                      string[j] = string[(i+1)+j];
  95.         }
  96.  
  97.     }
  98.     string[wordsize] = '\0';
  99.  
  100.     return string;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement