Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- #include <string.h>
- // Loads dictionary into memory, returning true if successful else false
- bool load(const char *dictionary)
- {
- // open the dictionary
- FILE *dict = fopen(dictionary, "r");
- if (dict == NULL)
- {
- fprintf(stderr, "Could not open\n");
- return 2;
- }
- // define node
- typedef struct node
- {
- // node has a boolean which if true means you got to the end of the word, if false you still have more letters to go
- bool is_word;
- // point to next node which is the child
- struct node *children[27];
- }
- node;
- node *root;
- root = malloc(sizeof(node));
- root->is_word = false;
- node *temp;
- temp = malloc(sizeof(node));
- temp = root;
- char s[45];
- node *new_node;
- for(int i=0; fgets(s, 45, dict) != NULL; i++)
- {
- for(int j=0, n = strlen(s); j < n - 1; j++)
- {
- int ascii = s[j] - 97;
- printf("%d ", ascii);
- if (temp->children[ascii] == NULL)
- {
- new_node = malloc(sizeof(node));
- }
- else if (temp->children[ascii] != NULL)
- {
- temp = new_node;
- }
- else
- {
- temp->is_word = true;
- }
- }
- printf("\n");
- }
- printf("This is the dictionary: %s\n", dictionary);
- free(root);
- fclose(dict);
- return false;
- }
- int main(int argc, char *argv[])
- {
- if (argc != 2)
- {
- printf("error");
- return 1;
- }
- char *dictionary = argv[1];
- load(dictionary);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement