please help !!
I cant comiple this code for a spell checker
when i build the project i get no de-bugg messages
however when i run the code it i get actual answer.exe has stopped working
i have attached the relevent files
except the word.txt is missing as it wouldnt let me attach it
JOHN-shirley 0 Newbie Poster
/** \file dictionary.h
* This file contains the datatypes needed to implement the dictionary, and the
* prototypes for functions visible to the rest of the code inside dictionary.c
*
* \author Chris Page <chris@starforge.co.uk>
* \version 1
*/
#ifndef _DICTIONARY_H // This is used to ensure that the header is only ever included once
#define _DICTIONARY_H 1
/** A structure that forms a node in a linked list of words.
*/
struct wordnode
{
struct wordnode *next; //!< A pointer to the next node in the list
char *word; //!< A pointer to the word associated with this node
};
/** A structure that represents the dictionary itself. This contains 26 linked
* list head pointers, one list for each character in the alphabet.
*/
typedef struct {
struct wordnode *headptrs[26]; //!< 26 list head pointers
} Dictionary;
Dictionary *create_dictionary(void);
void free_dictionary(Dictionary *dict);
int add_word(Dictionary *dict, char *word);
int find_word(Dictionary *dict, char *word);
void print_dictionary(Dictionary *dict);
#endif
/** \file project2.c
* Implementation of the file reading and spell-checking code for project 2.
* This uses the Dictionary datatype and the functions in the dictionary module
* to check words entered by the user on the command line.
*
* \author Chris Page <chris@starforge.co.uk>
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "dictionary.h"
/** The maximum size of a word in the dictionary
*/
#define LENGTH 200
/** Print the dictionary during spellchecking? If this is true, the dictionary
* is printed, if it is false it is not.
*/
#define PRINT_DICT 0
/** convert the specified string to lowercase. This function works in-place to
* convert the contents of the string to all lowercase characters
*
* @param str The string to convert to lowercase.
* @return A pointer to the string passed to the function.
*/
static char *strlower(char *str)
{
char *curr = str;
while((*curr = tolower(*curr)))
{
++curr;
}
return str;
}
/** Remove the trailing newline from the specified string, if it has one. If the
* string does not end in a newline then this will not modify the string.
*
* \param string The string to remove the newline from.
* \return A pointer to the string.
*/
static char *chomp(char *string)
{
int size = strlen(string);
int pos;
// move back down the string from the end, as there may be a CR, CR+LF or LF there...
for(pos = (size - 1); pos >= 0; --pos)
{
// If the character is a CR or LF, replace it with null
if(string[pos] == 0xD || string[pos] == 0xA)
{
string[pos] = 0;
}
else
{
return string;
}
}
return string;
}
int main(int argc, char **argv)
{
// Attempt to open the file containing the words to add to the dictionary
FILE *wordfile = fopen("words.txt", "r");
if(wordfile == NULL)
{
fprintf(stderr, "Unable to open words file.\n");
exit(1);
}
// Create an empty dictionary to read words into
Dictionary *dict = create_dictionary ;
// now read words into the dictionary
char buffer[LENGTH];
while(fgets(buffer, LENGTH, wordfile))
{
chomp(buffer); // string newlines from the end of buffer
strlower(buffer); // convert the string to lowercase
add_word(dict, buffer); // add the word to the dictionary
}
// Can close the file now, as it's no longer needed
fclose(wordfile);
// If the code is compiled with PRINT_DICT set to true, print the dictionary
#if PRINT_DICT
print_dictionary(dict);
#endif
int arg;
for(arg = 1; arg < argc; ++arg)
{
char *current = argv[arg]; // get a pointer to the current word for convenience
strlower(current); // convert the argument to lowercase
// does the word exist as it is?
if(find_word(dict, current))
{
printf("'%s' is OK\n", current);
// nope; is it a possible plural? If so, try without the s
}
else if(current[strlen(current) - 1] == 's')
{
current[strlen(current) - 1] = '\0'; // remove the 's' from the end
if(find_word(dict, current))
{
printf("Singular of '%ss' is OK\n", current); // the '%ss' is deliberate!
}
else
{
printf("'%ss' is not OK\n", current);
}
// Not found, and not a potential plural, so
}
else
{
printf("'%s' is not OK\n", current);
}
}
// release the dictionary
free_dictionary(dict);
return 0;
}
/** \file dictionary.c
* This file contains the implementation of the dictionary functions.
*
* \author Chris Page <chris@starforge.co.uk>
* \version 1
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
/** Allocate a new Dictionary structure and perform any initialisation
* tasks required to set the dictionary up.
*
* @return A pointer to an empty dictionary.
*/
Dictionary *create_dictionary(void)
{
Dictionary *newdict = (Dictionary *)calloc(1, sizeof(Dictionary));
if(newdict == NULL)
{
fprintf(stderr, "Unable to allocate memory for a new Dictionary.\n");
exit(1);
}
return newdict;
}
/** Release the memory used by a dictionary and any internal storage it may
* have associatd with it
*
* @param dict A pointer to the dictionary to free.
*/
void free_dictionary(Dictionary *dict)
{
int i;
// Loop over all the linked lists in the dictionary
for(i = 0; i < 26; ++i)
{
// get a pointer to the first item in the 'i'th linked list
struct wordnode *current = dict -> headptrs[i];
// traverse the linked list, freeing entries along the way
while(current != NULL)
{
struct wordnode *tmp = current -> next; // make a copy of the next pointer
free(current -> word); // release the memory storing the word
free(current); // and release the word itself
current = tmp; // move onto the next item in the list
}
}
// At this point all the linked lists have been freed, so the
// dictionary structure itself can be freed
free(dict);
}
/** Add the provided word to the dictionary, provided that the word does not
* already exist in the dictionary. This function checks whether the word
* pointed to by 'word' already exists in 'dict'. If it does not, the word
* is added to the storage inside dict. If the word is added to 'dict' this
* returns true, if the word is not added then it returns false.
*
* @param dict A pointer to the dictionary to add the word to.
* @param word The word to add to the dictionary
* @return true if the word was added, false otherwise.
*/
int add_word(Dictionary *dict, char *word)
{
// Do nothing if the word already exists in the dictionary
if(find_word(dict, word)) return 0;
// Allocate space for a new node
struct wordnode *newnode = (struct wordnode *)malloc(sizeof(struct wordnode));
if(newnode == NULL)
{
fprintf(stderr, "Unable to allocate memory for a new wordnode.\n");
exit(1);
}
// Allocate space for a copy of the word
newnode -> word = (char *)malloc(strlen(word) + 1);
if(newnode -> word == NULL)
{
fprintf(stderr, "Unable to allocate memory for a copy of the new word.\n");
exit(1);
}
// copy the word over
strcpy(newnode -> word, word);
// And now link the new node into the list
int listnum = word[0] - 'a';
newnode -> next = dict -> headptrs[listnum]; // next item is the current list head
dict -> headptrs[listnum] = newnode; // and update the head
return 1; // added the word, all done.
}
/** Attempts to find the provided word in the dictionary. Returns true if the
* word is found, false if it is not.
*
* @param dict A pointer to the dictionary to search in
* @param word The word to search for in the dictionary
* @return true if the word was found, false otherwise.
*/
int find_word(Dictionary *dict, char *word)
{
// Identify the list that should be searched
int listnum = word[0] - 'a';
// get a pointer to the head of the list
struct wordnode *current = dict -> headptrs[listnum];
// And now traverse the list looking for matches
while(current != NULL)
{
// if the words match, we've found the word
if(strcmp(word, current -> word) == 0)
{
return 1;
}
// Otherwise, move to the next item
current = current -> next;
}
// Get here an the whole list has been checked, return false
return 0;
}
/** Print the contents of the dictionary, one word per line.
*
* @param dict A pointer to the dictionary to print out.
*/
void print_dictionary(Dictionary *dict)
{
int i;
// Loop ovr all the lists in the dictionary
for(i = 0; i < 26; ++i)
{
// now traverse the list of items, printing out the words
struct wordnode *current = dict -> headptrs[i];
while(current != NULL)
{
printf("%s\n", current -> word); // print the word out
current = current -> next; // and move to the next
}
}
}
Moschops 683 Practically a Master Poster Featured Poster
Dictionary *dict = create_dictionary ;
Did you meanDictionary *dict = create_dictionary();
?
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.