0

I think my load() function is correct and I am getting Segmentation fault in check() function. This is my code for dictionary.c

#include <stdbool.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

long sizeofd=0;
#include "dictionary.h"
typedef struct node
{
    char name[LENGTH+1];
    struct node* next;
}node;

node* root[26];


/**
 * Returns true if word is in dictionary else false.
 */

bool check(const char* word)
{ 
    for(int i=0;word[i]!='\0';i++)
    {
        tolower(word[i]);
    }
    int index=word[0]-'a';


    if(!strcmp(root[index]->name,word))
    {
        return true;
    }
    else
    {node*temp2=root[index];
     if(!temp2)
        return false;
        while((temp2->next))
        {   temp2=temp2->next;
            if(!strcmp(temp2->name,word))
            {
                return true;
            }

        }
    }
    return false;
}

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{ 
    int ii=0;
    while(ii<26)
    {
    root[ii] = malloc(sizeof(node));
    root[ii] = NULL;
    ii++;
    }
    FILE* file = fopen(dictionary,"r");
    if(!file)
       {
        return false;


       }

    char c;
    char nm[LENGTH+1];
    int i=0,index;

   // temp=NULL;
    while((c = fgetc(file)) != EOF)
    { 
            if(i==0)
                strcpy(nm,"");
            if(c!='\n')
                {
                    nm[i]=c;
                    i++;

                }  

            else
            {
                nm[i]='\0';
                i=0;
                index=nm[0]-'a';

                if(!root[index])
                {  
                  node* temp=malloc(sizeof(node));
                  //  temp=malloc(sizeof(node));
                    root[index]=temp;
                    strcpy(root[index]->name,nm);
                    sizeofd++;
                    root[index]->next=NULL;
                  // free(temp);
                }
                else
                {   
                  //  temp=malloc(sizeof(node));
                    node*temp1=root[index];


                while(temp1->next!=NULL)
                    {
                        temp1=temp1->next;

                    }
                node* temp=malloc(sizeof(node));
                strcpy(temp->name,nm);
                temp1->next=temp;
                temp->next=NULL;
                sizeofd++;
              //  free(temp1);
             //  free(temp);

                }
            }
    }
    fclose(file);
  return true;                

}

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

    return sizeofd;
}

/**
 * Unloads dictionary from memory.  Returns true if successful else false.
 */
bool unload(void)
{
    // TODO
    return false;
}

Please tell me what's the problem!

2

As far as your check function is concerned, here is where you can make some improvements:

  1. Instead of

    for(int i=0;word[i]!='\0';i++)

why not

for (int i=0; i<strlen(word); i++)
  1. After converting all the letters in word to lower case, don't you want to store them in an array?

  2. Your use of strcmp is faulty. It's not boolean, strcmp returns 0 if it's a match, which means you can just return true if 0, false otherwise. This is assuming you've stored the word in lower case in an array.

  3. You want to navigate the list while strcmp!=0, stopping only when there's a match, presumably through a break command. And only when you reach the end of the list without finding a match do you return false.

1
  • Thanks I have made the changes, It's working now – Salil Jain Jun 13 '16 at 6:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .