Using C The purpose of this assignment is to give you practi
Using C++
The purpose of this assignment is to give you practice using dynamic memory allocation, c-string functions, sorting, and searching. You will write a spell checking program and test it.
Program Steps
Read a dictionary word file into an array of c-strings. You will need to dynamically allocate memory for each word in the dictionary. The word file contains 23,907 words.
Sort the dictionary.
Reading in a test file, one line at a time. Parse the line to get each individual word in the line. Search the dictionary word list (the array of c-strings) for each word in the line. If the word is not found, print a message. The search should be performed as a binary search.
Program Requirements
Use these files for your dictionary and the final spell check test.
Use an array of char pointers store your word file. Allocate memory dynamically to store each word in a char array. Store the dictionary words in lower case.
Use a temporary char array to hold each line in the test file. “Parse” each line into individual words for spell checking. Note, you may notuse the stringstream classes for this assignment.
Spell checking must ignore case. For example, “Hello” is the same as “hello”.
Program Output
Your output should looks quite similar to this:
Misspelled word, struggled on line 10
Misspelled word, consecrated on line 11
Misspelled word, unfinished on line 13
Misspelled word, nobly on line 14
Misspelled word, advanced on line 14
...
Program Hints
Follow the program steps. Write only one part of the program at a time. Test each part before you proceed to the next step. Do not continue if one part has a problem. Ask for help with a step, if you can\'t get it to work. Remember to allow plenty of time for this assignment.
Use a small dictionary and a small test file initially as you are developing your code.
Use strtok() for the parsing of each line.
Xcode users: There is a \ at the end of each line in the test file. You can suppress it by adding \"\ \" as a delimiter for strtok().
Your program should find between 8 and 12 misspelled words.
Solution
#include <stdio.h>
#include<string.h>
char dictionary[1000000];
char article[100000];
void spellCheck(char[], char[]);
int isLetter(char c);
void removePunc(char article[]);
void toLower( char article[]);
void lowerDictionary( char dictionary[]);
int artLength( char article[]);
void nextArticleWord(char article[], char articleWord[], int artLength, char dictionary[]);
int main(void) {
FILE* dict_file;
FILE* article_file;
int bytes_read;
char* p;
dict_file = fopen(\"american-english.txt\", \"r\");
if (dict_file == 0) {
printf(\"unable to open dictionary file \\\"american-english.txt\\\"\ \");
return -1;
}
article_file = fopen(\"article.txt\", \"r\");
if (article_file == 0) {
printf(\"unable to open file \\\"article.txt\\\"\ \");
return -1;
}
/* read dictionary */
p = dictionary;
p = fgets(p, 100, dict_file);
while (p != 0) {
while (*p != \'\\0\') {
p += 1;
}
p = fgets(p, 100, dict_file);
}
/* read article */
p = article;
bytes_read = fread(p, 1, 1000, article_file);
p += bytes_read;
while (bytes_read != 0) {
bytes_read = fread(p, 1, 1000, article_file);
p += bytes_read;
}
*p = 0;
spellCheck(article, dictionary);
}
int articlePosition =0;
int dictionaryPosition = 0;
void spellCheck(char article[], char dictionary[]) {
char articleWord[50];
char dictionaryWord[50];
int articleLength = artLength(article);
removePunc(article);
toLower(article);
lowerDictionary(dictionary);
nextArticleWord(article, articleWord, articleLength, dictionary);
}
void nextDictionaryWord(char dictionary[], char dictionaryWord[]){
int i;
for(i =0; dictionary[dictionaryPosition] != \'\ \'; i++){
dictionaryWord[i] = dictionary[dictionaryPosition];
dictionaryPosition++;
}
}
int isLetter(char c){
if ( (c>=\'a\'&&c<=\'z\') || (c>=\'A\'&&c<=\'Z\'))
return 1;
return 0;
}
void removePunc(char article[]){
int i, j=0;
for ( i =0; article[i] != 0; i++){
if (isLetter(article[i])){
article[j] = article[i];
j++;
}
else if (!isLetter(article[i])){
article[j] = \' \';
j++;
}
}
}
void toLower( char article[]){
int i=0;
for( i; article[i] != 0; i++){
if ( article[i] >= \'A\' && article[i] <=\'Z\')
article[i] = article[i] + 32;
}
}
void lowerDictionary( char dictionary[]){
int i=0;
for(i; dictionary[i] != 0; i++){
if (dictionary[i] >= \'A\' && dictionary[i] <= \'Z\'){
dictionary[i] = dictionary[i] + 32;
}
}
}
int articleLength( char article[] ){
int count=0;
while (article[count] != 0)
count++;
return count;
}
void nextArticleWord(char article[], char articleWord[], int articleLength, char dictionaryWord[], char dictionary[]){
int j, i;
check:
while(!isLetter(article[articlePosition])){
if (article[articlePosition] == 0){
return;
}
articlePosition++;
}
for(j=0; article[articlePosition] != \' \' || articlePosition == articleLength; j++){
articleWord[j] = article[articlePosition];
articlePosition++;
}
if (strlen(articleWord)<2){
goto check;
}
articleWord[j+1] = 0;
//dictionary search
while (!strncmp(articleWord, dictionaryWord,strlen(articleWord))){
nextDictionaryWord(dictionary, dictionaryWord);
}
if(strncmp(articleWord, dictionaryWord,strlen(articleWord)))
return;
printf(articleWord);
}



