0

i dont know what am doing wrong in my load function but my speller says it cant load, please shed some light on where am doing wrong. * Implements a dictionary's functionality. */

    #include <stdbool.h>

#include "dictionary.h"
#include <string.h>
#include <ctype.h>
//make a trie node
typedef struct node
{
    bool is_word;
    struct node*next_char[27];
}node;

/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
// TODO
int INDEX;
struct node*Pcrawl;
for(int i=0;i<=(strlen(word)+1);i++)
{
  INDEX=tolower(word[i])-'a';
  if(pcrawl->next_char[INDEX])
  {
   Pcrawl=pcrawl->next_char[INDEX];
  }
  if(Pcrawl->next_char[word[i]]=='\0')
  {
   Pcrawl->is_word=true;
  }
return true;
}
 return false;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{
// TODO
FILE *dict_words=fopen(dict_words,"r");
char word[LENGHTH+1];
//make new node and initialize to NULLs 
 struct node*gnew_node(void)
 {
   struct node*children=NULL;
   children=(node*)malloc(sizeof(node));
   if(children==NULL)
   {
       unload();
       return false;
   }
   children->is_word=false;
   for(int i=0;i<=26;i++)
   {
       children->next_char[i]=NULL;
   }
   return children;
  }
  //scan dictionary word by word

  while(fscanf(dict_words,%s,word)!=EOF)
  {
    //declare a root pointer
    struct node*root_ptr=(node)malloc(sizeof(node));
    if(root_ptr==NULL)
    {
        unload();
        return false;
    }
   struct node*trav=root_ptr;
   for(int i=0;i<=strlen(word);i++)
   {
       if(trav->next_char[islower(word[i])-'a']==NULL)
       {
       trav->next_char[islower(word[i])-'a']=gnew_node();
       trav=trav->next_char[islower(word[i])-'a'];
       }
       if(next_char[islower(word[i])-'a']=='\0'")
       {
        trav->is_word;
       }
       }
       return true;
      }
        return false;
}

   /**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
// TODO

unsigned int my_size= load(dictionary);
if(my_size==0)
{
    return 0;
}
FILE *fp=fopen(dictionary,"r");
if(fp)
{
    char ch;
    int word_count=0;
    while((ch=fgetc(fp))!=EOF)
    {//increment word count
        if(ch=='\n')
        ++word_count;
    }
    return (word_count);
   }
  return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{
// TODO
const char* dictionary;
struct node* Pnode;
Pnode=root_ptr;
if(Pnode)
{
    for(int i=0;i<=strlen(dictionary);i++)
    {
        if(Pnode->next_char[dictionary[i]-'a')
        for(int j=strlen(dictionary);j>=0;j--)
        {
            free(Pnode->next_char[dictionary[j]-'a']);
        }
        if(Pnode->next_char[dictionary[i]-'a']==NULL)
        unload(Pnode->next_char[dictionary[i]-'a']);

    }
    free(children);
    free(root_ptr);
    return true;
    }
     return false;
    }
5
  • That code, the way it is, will not compile on the IDE or any C standard compiler. You're declaring a function, gnew_node, nested inside your load() function. That's not legal C code. – Irene Oct 9 '17 at 4:32
  • made the changes in gnew but still the thing cant load: pastebin.com/8unMy6qY – TabithaWamucii Oct 9 '17 at 11:25
  • And are you running it correctly and from the correct folder? The only possibility that your load() function will return false is if the file can't actually be opened. So, post a screenshot of how you're running it – Irene Oct 9 '17 at 20:37
  • @Irene how do i post a screenshot,here? i have no idea.but after make i pass this command: ./speller dictionaries/small text – TabithaWamucii Oct 14 '17 at 17:43
  • @Irene thank you, i figured out my problem.its not in command and the code,it on the files.my offline ide had duplicate files – TabithaWamucii Oct 14 '17 at 18:19

You must log in to answer this question.

Browse other questions tagged .