I have been asked to program a spellchecker in C for an assignment. I am quite new to C and programming in general, so I have decided to start by writing a program that does the following:
- Reads words into an array from a dictionary text file.
- Reads words into an array from a sample file that needs to be spellchecked.
- Compares whether or not each word form the sample file is in the dictionary using a binary search algorithm.
Here is my code so far:
#include <stdio.h>
#include <string.h>
int read_words(char *dict[20]);
int read_text(char *sample[3]);
int comparison(char *dict[20], char *sample[3]);
int main()
{
char *dict[20]; //pointer to array 'dict'
char *sample[3]; // pointer to array 'sample'
read_words(dict);
read_text(sample);
comparison(dict, sample);
}
int read_words(char *dict[20]) //copies each word from the file 'words.txt' into array 'dict'
{
FILE *words_ptr; //pointer for words.txt
int i;
char temp_word[20];
char *new_word;
words_ptr = fopen( "words.txt", "r" );
if( words_ptr != NULL )
{
printf( "File words.txt opened\n");
i=0;
while (fgets( temp_word, 20, words_ptr ))
{
new_word = (char*)calloc(strlen(temp_word), sizeof(char)); //ensuring new_word will be the right size
strcpy(new_word, temp_word); //copy contents of temp_word to new_word
dict[i] = new_word; //copy contents of new_word to i'th element of dict array
printf("printing out dict[%d]: %s\n", i, dict[i]);
i++;
}
printf("printing out dictionary1: %s\n", dict[1]);
fclose( words_ptr );
return 0;
}
else {printf( "Unable to open file words.txt\n" ); return 1;}
}
int read_text(char *sample[3]) //copies each word from the file 'text.txt' into array 'sample'
{
//this works exactly the same way as the read_words function
FILE *text_ptr;
int j;
char temp_text[20];
char *new_text;
text_ptr = fopen( "text.txt", "r" );
if( text_ptr != NULL )
{
printf( "File text.txt opened\n");
j=0;
while (fgets( temp_text, 20, text_ptr ))
{
new_text = (char*)calloc(strlen(temp_text), sizeof(char));
strcpy(new_text, temp_text);
sample[j] = new_text;
printf("printing out sample[%d]: %s\n", j, sample[j]);
j++;
}
printf("printing out sampleee1: %s\n", sample[1]); //testing that it prints out sample[1] and not whichever the last sample word was. Can be removed from final program.
fclose( text_ptr );
return 0;
}
else {printf( "Unable to open file text.txt\n" ); return 1;}
}
int comparison(char *dict[20], char *sample[3]) //comparing one word from each array with the other and checking if they are the same
{
char *min, *max, *mid; //minimum value, maximum value, mid-point value
min = dict[0];
max = dict[20];
mid = min +(max-min)/2;
//performing the binary search
while((min <= max) && (*mid != sample[0]))
{
if (sample[0] < *mid)
{
max = mid -1;
mid = min +(max-min)/2;
}
else
{
min = mid + 1;
mid = min +(max-min)/2;
}
}
if (*mid == sample[0])
{ printf("\n %d found!", sample[0]); }
else {printf("\n %d not found!", sample[0]); }
return 0;
}
I get a few error messages at lines 89, 91 and 103 saying: "warning: comparison between pointer and integer".
I can see why I am getting these messages but I do not know how to change the code to do what I want it to do.
I can see a problem with this line (line 89):
while((min <= max) && (*mid != sample[0]))
I am not able to compare mid with sample[0] as they are different types. I want mid to be the middle word from the dict[]array so that I can compare the two values.
I have seen similar code working for when doing a binary search on integers, but am not sure if this is possible when doing a search on strings.
I would appreciate any advice, although I have been told it is possible to do this using something line binary search, or binary search trees, so I would be grateful to know something similar to this. I think a hash table might be a bit beyond me at this stage although I am aware that this is another method.
Thanks