Skip to main content
deleted 344 characters in body
Source Link
   /**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * 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]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]word[i]>='A' && word[i]<='Z')
        a=(int)word[i]-65;
    else if(word[i]>='a' && word[i]<='z')
      a=word[i]  a=(int)word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else 
        tmp=tmp->children[a];
    }
    if(tmp->is_word==true)
        return true;
    else
        return false;
tmp->is_word;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
char name[LENGTH+1];
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (!feoffscanf(file,"%s\n",name)>0)
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]i=0;name[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]name[i]))
            a=(int)word[i]name[i]-97;
        else
            a=26;
        if(tmp->children[a]==NULL)
        {
            newnode=calloc(1, sizeof(node));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }
        }
        tmp->is_word=true;
    }
    // TODO
    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    if(count!=0)
        return count;
    else
        return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    for(int i=0;i<27;i++)
        if(root->children[i]!=NULL)
            delNode(root->children[i]);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

When
I
grow
up
I
want
to
be
principal
or
caterpillar
I
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 18 15
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.1819
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.1718
TIME IN TOTAL: 0.3537

/**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * 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]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else 
        tmp=tmp->children[a];
    }
    if(tmp->is_word==true)
        return true;
    else
        return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;
        if(tmp->children[a]==NULL)
        {
            newnode=calloc(1, sizeof(node));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }
        }
        tmp->is_word=true;
    }
    // TODO
    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    if(count!=0)
        return count;
    else
        return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    for(int i=0;i<27;i++)
        if(root->children[i]!=NULL)
            delNode(root->children[i]);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

When
I
grow
up
I
want
to
be
principal
or
caterpillar
I
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 18
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.18
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.17
TIME IN TOTAL: 0.35

   /**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(word[i]>='A' && word[i]<='Z')
        a=(int)word[i]-65;
    else if(word[i]>='a' && word[i]<='z')
        a=(int)word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else 
        tmp=tmp->children[a];
    }
    return tmp->is_word;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
char name[LENGTH+1];
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (fscanf(file,"%s\n",name)>0)
    {
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;name[i]!='\0';i++)
        {
            int a;
        if(isalpha(name[i]))
            a=(int)name[i]-97;
        else
            a=26;
        if(tmp->children[a]==NULL)
        {
            newnode=calloc(1, sizeof(node));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else
            tmp=newnode;
        }
        tmp->is_word=true;
    }
    // TODO
    fclose(file);
    return true;
}

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

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    delNode(root);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

When
grow
up
want
to
be
principal
or
caterpillar
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 15
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.19
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.18
TIME IN TOTAL: 0.37

deleted 228 characters in body; edited tags; edited title
Source Link

pset5:(speller) Segmentation Fault.Check function does not correctly identify misspelled words

valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function


/**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h> 

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    for(int i=0;word[i]!='\0';i++)
        tolower(word[i]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else if(tmp->children[a]!=NULL)
        {
            tmp=tmp->children[a];
        }
    if(i==(strlen(word)-1))
    {
        if(tmp->is_word==true)
            return true;
        else
            return false;
        
    }
    }
return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;

Line 76

        if(tmp->children[a]==NULL) //line76
        {
            newnode=mallocnewnode=calloc(1, sizeof(newnodenode));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }
        if(i==(strlen(word)-1))}
            tmp->is_word=true;
        }
        
   // }TODO
    // TODOfclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    if(count!=0)
        return count;
    else
        return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    for(int i=0;i<27;i++)
        if(root->children[i]!=NULL)
            delNode(root->children[i]);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

OUTPUT

~/workspace/pset5/speller/ (master) $ ./speller texts/ralph.txt

MISSPELLED WORDS

When
I
grow
up
I
want
to
be
principal
or
caterpillar
I
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 18
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.18
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.17
TIME IN TOTAL: 0.35

pset5:(speller) Segmentation Fault.

valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function


#include <stdbool.h>
#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    for(int i=0;word[i]!='\0';i++)
        tolower(word[i]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else if(tmp->children[a]!=NULL)
        {
            tmp=tmp->children[a];
        }
    if(i==(strlen(word)-1))
    {
        if(tmp->is_word==true)
            return true;
        else
            return false;
        
    }
    }
return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;

Line 76

        if(tmp->children[a]==NULL) //line76
        {
            newnode=malloc(sizeof(newnode));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }
        if(i==(strlen(word)-1))
            tmp->is_word=true;
        }
        
    }
    // TODO
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    if(count!=0)
        return count;
    else
        return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    for(int i=0;i<27;i++)
        if(root->children[i]!=NULL)
            delNode(root->children[i]);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

pset5:(speller) Check function does not correctly identify misspelled words

/**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h> 

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * 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]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else 
        tmp=tmp->children[a];
    }
    if(tmp->is_word==true)
        return true;
    else
        return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;
        if(tmp->children[a]==NULL)
        {
            newnode=calloc(1, sizeof(node));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }
        }
        tmp->is_word=true;
    }
    // TODO
    fclose(file);
    return true;
}

/**
 * Returns number of words in dictionary if loaded else 0 if not yet loaded.
 */
unsigned int size(void)
{
    // TODO
    if(count!=0)
        return count;
    else
        return 0;
}

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    for(int i=0;i<27;i++)
        if(root->children[i]!=NULL)
            delNode(root->children[i]);
    return true;
    
}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

OUTPUT

~/workspace/pset5/speller/ (master) $ ./speller texts/ralph.txt

MISSPELLED WORDS

When
I
grow
up
I
want
to
be
principal
or
caterpillar
I
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 18
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.18
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.17
TIME IN TOTAL: 0.35

added 93 characters in body; edited title
Source Link

pset5:(speller) Segmentation Fault. valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function

valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function


#include <stdbool.h>
 
#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    for(int i=0;word[i]!='\0';i++)
        tolower(word[i]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else if(tmp->children[a]!=NULL)
        {
            tmp=tmp->children[a];
        }
    if(i==(strlen(word)-1))
    {
        if(tmp->is_word==true)
            return true;
        else
            return false;
        
    }
    }
return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;

pset5:(speller) Segmentation Fault. valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function

#include <stdbool.h>
 
#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    for(int i=0;word[i]!='\0';i++)
        tolower(word[i]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else if(tmp->children[a]!=NULL)
        {
            tmp=tmp->children[a];
        }
    if(i==(strlen(word)-1))
    {
        if(tmp->is_word==true)
            return true;
        else
            return false;
        
    }
    }
return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;

pset5:(speller) Segmentation Fault.

valgrind shows error at line 76 if(tmp->children[a]==NULL) in load function


#include <stdbool.h>
#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    // TODO
    for(int i=0;word[i]!='\0';i++)
        tolower(word[i]);
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(isalpha(word[i]))
            a=word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else if(tmp->children[a]!=NULL)
        {
            tmp=tmp->children[a];
        }
    if(i==(strlen(word)-1))
    {
        if(tmp->is_word==true)
            return true;
        else
            return false;
        
    }
    }
return false;

}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    while (!feof(file))
    {
        char word[LENGTH+1];
        fscanf(file,"%s\n",word);
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;word[i]!='\0';i++)
        {
            int a;
        if(isalpha(word[i]))
            a=(int)word[i]-97;
        else
            a=26;
Source Link
Loading