I have written a program on my linux computer, i wrote a program such that it takes user input to add a word to trie, and again user input to search in a trie, and it had worked perfectly!
But, when i converted it accordingly to speller requirements , i am getting a segmentation fault. Could someone help me with this?
/**
* Implements a dictionary's functionality.
*/
#include "dictionary.h"
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <stdbool.h>
#include<ctype.h>
struct trie
{
bool flag;
struct trie *tarr[27];
};
int free_word(struct trie *tnew);
int add_word(char *a,struct trie *tnew);
int l = 0,l1=0,l2 = 0,count=0;
int se1_word(char *a,struct trie *tnew);
struct trie tnew;
/**
* Returns true if word is in dictionary else false.
*/
bool check(const char *word)
{
char dest[LENGTH+5];
l2 = strlen(word);
strcpy(dest,word);
int a6 = se1_word(dest,&tnew);
if ( a6 == 1)
{
return true;
}
else
{
return false;
}
}
/**
* Loads dictionary into memory. Returns true if successful else false.
*/
bool load(const char *dictionary)
{
tnew.flag = false;
int i;
struct trie **dtrie = tnew.tarr;
for(i=0;i<27;i++)
{
dtrie[i] = NULL;
}
FILE *fp = fopen(dictionary,"r");
if ( fp == NULL)
{
return false;
}
char a[LENGTH+5];
while(fgets(a,LENGTH+1,fp) != NULL)
{
count += 1;
l1 = 0;
l = strlen(a);
add_word(a,&tnew);
}
fclose(fp);
return true;
}
/**
* Returns number of words in dictionary if loaded else 0 if not yet loaded.
*/
unsigned int size(void)
{
if ( count != 0)
{
return count;
}
else
{
return 0;
}
}
/**
* Unloads dictionary from memory. Returns true if successful else false.
*/
bool unload(void)
{
int k1 = free_word(&tnew);
if ( k1 == 1)
{
return true;
}
else
{
return false;
}
}
int se1_word(char *a,struct trie *tnew)
{
//printf("add:%u\n",tnew);
l1 += 1;
//printf("%s\n",a+l1);
//printf("..l:%d,l1:%d\n",l,l1);
//printf("tnew:%u\n",tnew);
if ( tnew == NULL) //for word of same size,but different words.
{
//printf("l1:%d,l2:%d\n",l1,l2);
return -1;
}
if ( l1 == l2)
{
if(tnew->flag == true)
{
return 1;
}
else
{
return -1;
}
}
if ( a[l1] != '\'')
{
a[l1] = tolower(a[l1]);
}
int i1 = a[l1] - 'a';
if ( a[l1] == '\'')
{
// printf("haa\n");
i1 = 26;
}
struct trie **arr = tnew->tarr;
int k1 = se1_word(a,arr[i1]);
return k1;
}
int free_word(struct trie *tnew)
{
if ( tnew->flag == true)
{
return 1;
}
if ( tnew == NULL)
{
return 1;
}
int i =0;
struct trie **array = tnew->tarr;
for(i=0;i<27;i++)
{
if ( array[i] != NULL)
{
free_word(array[i]);
free(array[i]);
}
}
return 1;
}
int add_word(char *a,struct trie *tnew)
{
printf("%s\n",a+l1);
printf("l:%d,l1:%d\n",l,l1);
//printf("add:%u\n",tnew);
if ( l == l1)
{
tnew->flag = true;
return 0;
}
if ( a[l1] != '\'')
{
a[l1] = tolower(a[l1]);
}
int a1 = a[l1] - 'a';
if ( a[l1] == '\'')
{
//printf("haa\n");
a1 = 26;
}
l1 += 1;
struct trie **poi = (tnew)->tarr;
struct trie **arr = poi;
struct trie *new1 = calloc(1,sizeof(struct trie));
struct trie **po2 = (new1->tarr);
new1 ->flag = false;
int i1;
for(i1=0;i1<27;i1++)
{
po2[i1] = NULL;
}
printf("add:%p\n",arr[a1]);
if(arr[a1] == NULL)
{
arr[a1] = new1;
}
int k1 = add_word(a,arr[a1]);
return k1;
}
TIA!