Skip to main content
added 552 characters in body
Source Link
Yuri Laguardia
  • 2.6k
  • 1
  • 10
  • 17

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

#include "dictionary.h"

//definition of struct type typedef struct node { bool is_word; struct node* children[27]; } node;

int index; node* root;

/**

  • Returns true if word is in dictionary else false. / bool check(const char word) { node* trav = root;

    for(int i=0; i<strlen(word); i++) { //assign each element an index, including apostrophes if(word[i]== ''') { index = 26; }

     else
     {
     index = tolower(word[i])-'a';
     } 
           //check if not in dictionary
     if(trav->children[index] == NULL) //SEGMENTATION FAULT!
     {
       return false;
     }
    
     trav=trav -> children[index];
    

    }

     if(trav->is_word == true)
     {
        return true;
     }
    

    return false; }

/**

  • Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) {

    //gdefined root node node* root=NULL; //assign memory root = malloc(sizeof(node));

    //Open file

// open input file FILE* dict = fopen(dictionary,"r");

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

#include "dictionary.h"

//definition of struct type
   typedef struct node
    {
        bool is_word;
        struct node* children[27];
    }
    node;
    
int index;
node* root;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
  node* trav = root;
    
   for(int i=0; i<strlen(word); i++)
   {
        //assign each element an index, including apostrophes
        if(word[i]== '\'')
        {
         index = 26;
        }
        
        else
        {
        index = tolower(word[i])-'a';
        } 
              //check if not in dictionary
        if(trav->children[index] == NULL) //SEGMENTATION FAULT!
        {
          return false;
        }
    
        trav=trav -> children[index];
    }
        
        if(trav->is_word == true)
        {
           return true;
        }
          
    return false;
   }

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    
    //gdefined root node
    node* root=NULL;
    //assign memory
    root = malloc(sizeof(node));
    
    //Open file
    
  // open input file 
    FILE* dict = fopen(dictionary,"r");
    
    //Check raw data is available
    if (dict == NULL)
    {
         fclose(dict);
        fprintf(stderr, "Could not open %s.\n","dictionary" );
        return 1;
    }
    
    //for each word read in, iterate through trie
    
    char temp[LENGTH+1];
    
    int wordcount = 0;
    
    while(fscanf(dict,"%s\n",temp) != EOF)
    {
        
    //Count words as we go
     
       wordcount++;
       
    //define traversal node
       
       node* trav = root;
        
       for(int i=0; i < strlen(temp); i++)
       
        {
            //assign each element an index, including apostrophes
        if(temp[i]== '\'')
        {
         index = 26;
        }
       else
        {
        index = tolower(temp[i])-'a';
        } 
       
        //if NULL malloc new node, have children i point to it
       if(trav->children[index]==NULL)
       {
          trav->children[index] = malloc(sizeof(node));
        }
       
        else
        {
         //if not NULL move to node and continue
        
            trav = trav->children[index];
        }
        
        //at end of work, set is_word to true
        
        if(i == strlen(temp)-1)
        {
            trav->is_word = true;
        }
        }
    
    }
    printf("%i", wordcount);
    return true;
}

/**

}

/**

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

#include "dictionary.h"

//definition of struct type typedef struct node { bool is_word; struct node* children[27]; } node;

int index; node* root;

/**

  • Returns true if word is in dictionary else false. / bool check(const char word) { node* trav = root;

    for(int i=0; i<strlen(word); i++) { //assign each element an index, including apostrophes if(word[i]== ''') { index = 26; }

     else
     {
     index = tolower(word[i])-'a';
     } 
           //check if not in dictionary
     if(trav->children[index] == NULL) //SEGMENTATION FAULT!
     {
       return false;
     }
    
     trav=trav -> children[index];
    

    }

     if(trav->is_word == true)
     {
        return true;
     }
    

    return false; }

/**

  • Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) {

    //gdefined root node node* root=NULL; //assign memory root = malloc(sizeof(node));

    //Open file

// open input file FILE* dict = fopen(dictionary,"r");

//Check raw data is available
if (dict == NULL)
{
     fclose(dict);
    fprintf(stderr, "Could not open %s.\n","dictionary" );
    return 1;
}

//for each word read in, iterate through trie

char temp[LENGTH+1];

int wordcount = 0;

while(fscanf(dict,"%s\n",temp) != EOF)
{
    
//Count words as we go
 
   wordcount++;
   
//define traversal node
   
   node* trav = root;
    
   for(int i=0; i < strlen(temp); i++)
   
    {
        //assign each element an index, including apostrophes
    if(temp[i]== '\'')
    {
     index = 26;
    }
   else
    {
    index = tolower(temp[i])-'a';
    } 
   
    //if NULL malloc new node, have children i point to it
   if(trav->children[index]==NULL)
   {
      trav->children[index] = malloc(sizeof(node));
    }
   
    else
    {
     //if not NULL move to node and continue
    
        trav = trav->children[index];
    }
    
    //at end of work, set is_word to true
    
    if(i == strlen(temp)-1)
    {
        trav->is_word = true;
    }
    }

}
printf("%i", wordcount);
return true;

}

/**

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

#include "dictionary.h"

//definition of struct type
   typedef struct node
    {
        bool is_word;
        struct node* children[27];
    }
    node;
    
int index;
node* root;

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
  node* trav = root;
    
   for(int i=0; i<strlen(word); i++)
   {
        //assign each element an index, including apostrophes
        if(word[i]== '\'')
        {
         index = 26;
        }
        
        else
        {
        index = tolower(word[i])-'a';
        } 
              //check if not in dictionary
        if(trav->children[index] == NULL) //SEGMENTATION FAULT!
        {
          return false;
        }
    
        trav=trav -> children[index];
    }
        
        if(trav->is_word == true)
        {
           return true;
        }
          
    return false;
   }

/**
 * Loads dictionary into memory.  Returns true if successful else false.
 */
bool load(const char* dictionary)
{
    
    //gdefined root node
    node* root=NULL;
    //assign memory
    root = malloc(sizeof(node));
    
    //Open file
    
  // open input file 
    FILE* dict = fopen(dictionary,"r");
    
    //Check raw data is available
    if (dict == NULL)
    {
         fclose(dict);
        fprintf(stderr, "Could not open %s.\n","dictionary" );
        return 1;
    }
    
    //for each word read in, iterate through trie
    
    char temp[LENGTH+1];
    
    int wordcount = 0;
    
    while(fscanf(dict,"%s\n",temp) != EOF)
    {
        
    //Count words as we go
     
       wordcount++;
       
    //define traversal node
       
       node* trav = root;
        
       for(int i=0; i < strlen(temp); i++)
       
        {
            //assign each element an index, including apostrophes
        if(temp[i]== '\'')
        {
         index = 26;
        }
       else
        {
        index = tolower(temp[i])-'a';
        } 
       
        //if NULL malloc new node, have children i point to it
       if(trav->children[index]==NULL)
       {
          trav->children[index] = malloc(sizeof(node));
        }
       
        else
        {
         //if not NULL move to node and continue
        
            trav = trav->children[index];
        }
        
        //at end of work, set is_word to true
        
        if(i == strlen(temp)-1)
        {
            trav->is_word = true;
        }
        }
    
    }
    printf("%i", wordcount);
    return true;
}

/**
Source Link

PSET5 - Trie Check Segmentation Fault

I have been trying with this code for some time and tried numerous permutations, however I am still struggling to get anywhere! I am very new to coding so please give any advice that will improve my skills. The code is as follows (segmentation fault indicated in code comments):

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

#include "dictionary.h"

//definition of struct type typedef struct node { bool is_word; struct node* children[27]; } node;

int index; node* root;

/**

  • Returns true if word is in dictionary else false. / bool check(const char word) { node* trav = root;

    for(int i=0; i<strlen(word); i++) { //assign each element an index, including apostrophes if(word[i]== ''') { index = 26; }

     else
     {
     index = tolower(word[i])-'a';
     } 
           //check if not in dictionary
     if(trav->children[index] == NULL) //SEGMENTATION FAULT!
     {
       return false;
     }
    
     trav=trav -> children[index];
    

    }

     if(trav->is_word == true)
     {
        return true;
     }
    

    return false; }

/**

  • Loads dictionary into memory. Returns true if successful else false. / bool load(const char dictionary) {

    //gdefined root node node* root=NULL; //assign memory root = malloc(sizeof(node));

    //Open file

// open input file FILE* dict = fopen(dictionary,"r");

//Check raw data is available
if (dict == NULL)
{
     fclose(dict);
    fprintf(stderr, "Could not open %s.\n","dictionary" );
    return 1;
}

//for each word read in, iterate through trie

char temp[LENGTH+1];

int wordcount = 0;

while(fscanf(dict,"%s\n",temp) != EOF)
{
    
//Count words as we go
 
   wordcount++;
   
//define traversal node
   
   node* trav = root;
    
   for(int i=0; i < strlen(temp); i++)
   
    {
        //assign each element an index, including apostrophes
    if(temp[i]== '\'')
    {
     index = 26;
    }
   else
    {
    index = tolower(temp[i])-'a';
    } 
   
    //if NULL malloc new node, have children i point to it
   if(trav->children[index]==NULL)
   {
      trav->children[index] = malloc(sizeof(node));
    }
   
    else
    {
     //if not NULL move to node and continue
    
        trav = trav->children[index];
    }
    
    //at end of work, set is_word to true
    
    if(i == strlen(temp)-1)
    {
        trav->is_word = true;
    }
    }

}
printf("%i", wordcount);
return true;

}

/**