Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Implements a dictionary's functionality.
- */
- #include <stdbool.h>
- #include <stdio.h>
- #include "dictionary.h"
- #include <ctype.h>
- #include <stdlib.h>
- const int CASE = 'a';
- typedef struct node{
- bool is_word ;
- struct node *children[27];
- }node;
- node *rootNode;
- FILE *inFile = NULL;
- /**
- * Returns true if word is in dictionary else false.
- */
- bool check(const char *word)
- {
- if(rootNode != NULL){
- while(*word != '\0'){
- char wordChar = tolower(*word);
- if(rootNode->children[wordChar - CASE] != NULL){
- rootNode = rootNode->children[wordChar - CASE];
- }
- word++;
- }
- if(rootNode->is_word != true){
- return false;
- }
- }
- return true;
- }
- // void insertNode(char *word){
- // node *currentNode = rootNode;
- // while(*word != '\0'){
- // if(currentNode->children[*word - CASE] == NULL){
- // currentNode->children[*word - CASE] = malloc(sizeof(node));
- // }
- // currentNode = currentNode->children[*word - CASE];
- // word++;
- // }
- // currentNode->is_word = true;
- // }
- /**
- * Loads dictionary into memory. Returns true if successful else false.
- */
- bool load(const char *dictionary)
- {
- if(rootNode == NULL){
- rootNode = malloc(sizeof(node));
- }
- inFile = fopen(dictionary , "r");
- char *word = malloc(sizeof(char));
- if(!inFile){
- printf("Could not find file");
- return false;
- }
- while(fscanf(inFile, "%s", word) != EOF){
- }
- return true;
- }
- /**
- * Returns number of words in dictionary if loaded else 0 if not yet loaded.
- */
- unsigned int size(void)
- {
- if(inFile == NULL){
- return 0;
- }
- rewind(inFile);
- char *store = malloc(sizeof(char));
- unsigned int counter = 0;
- if(rootNode == NULL){
- return 0;
- }
- while(fscanf(inFile, "%s", store) != EOF){
- if(check(store)){
- counter++;
- }
- }
- return counter;
- }
- /**
- * Unloads dictionary from memory. Returns true if successful else false.
- */
- bool unload(void)
- {
- // TODO
- return false;
- }
- int main(){
- load("dictionaries/small");
- if(check("zealousness")){
- printf("word found");
- }else{
- printf("Not found");
- }
- printf(" \nnumber of words %d\n", size());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement