Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Refresher: Anagram Solver am program that, using appropriate data st

ID: 664305 • Letter: P

Question

Programming Refresher: Anagram Solver am program that, using appropriate data struc ord is a permutation of 1etters that u rd of arbitrary length as input. your program shall search a dicti each letter exactly once to form a new word. Given a anagram of a hich th be d d b the d nary, tak d dete f the the g d. A sligh pp ake all p f the h the d nary for th The perfo f eith uld be b d of d b ed slightly gh this pp d b pfu he fa f a facto his by firs rting the lie time for an d dictionary. enabling logarithmic-time binary sear h of the Approach One possibly better approach to solving this problem is to cre hat hashes th fth of the letters you d. E ven this may not be ideal t gn th tter A. 2, C n, b d" G uld hash to the number tructu hash 8-8-8 24, b uld b diffe d. Altha h thi h hash tab hat hand inked f possib que hashing do th 32 hich h b f a 32-b ts a diffe ds. A til Z gth of 32 nly hold. p to 2 will hash h that A The p f th der p f th d. Mi fth will b this prob f A h table dat tructure that nked if cha ch ch tring th ching to the appropriate linked list that corresponds to that hash value. You will m hash entry, and each linked list within the hash Using this approach, you can iterate over each word in the dictionary. compute i ats "hash val d add h ry: furth hould maintain vour hash rted ched binary and then search that linked list in the hash table for your anagrams. Be sure to his, y ch sp will be greatly reduced. O our input word, you can compute its "hash value ll, validate tha the linked list is indeed an gram has the d and h lett f th hash val y repr optimizations a know automatically that a gram of another wor as main eparately and reduce the search space further. d to check fo d that he hash, b will b ort th p verify that the deed. y, y prog Scrabble Solver Once this is working, creat a copy of your program and modify it into a cros or, if you wish to use the original program, a d an input parameter to run in cross-word mode ll perfo search as above, but you will force on letter to be in a ll pe ch fo f th hich the third lett tati Reading from a File nfamil C, h ppet th ds the d nary file, line by lin d prints each dard /sh he d

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

int
     main(void)
     {
         FILE * dict;
         char * line = NULL;
         size_t read;
         int line_size;

       
       //check if file exists
     dict = fopen("/usr/share/dict/words", "r");
       if (dict == NULL)
            printf("file does not exists %s", filename);
                  return 0;
       }
      
        
     
       //read line by line
       const size_t line_size = 300;
       char* line = malloc(line_size);
       while (fgets(line, line_size, dict) != NULL){
           printf(line);
}