**PLEASE READ THE ENTIRE PROBLEM BEFORE ANSWERING. Write a program that implemen
ID: 3532928 • Letter: #
Question
**PLEASE READ THE ENTIRE PROBLEM BEFORE ANSWERING.
Write a program that implements a simple spell checker in C++. The spell checker should work as follows: prompt the user to enter a file name to spell check, and then the program will parse the file and check if there are any tokens that are not in the dictionary. Any words that are not found in the dictionary will be printed on the screen. Note that you do not need to worry about sorting words or prompting the user for corrections.
The program will be tested with two .txt files. One will be random sentences (to be spell checked) and the other will be the dictionary.
This program MUST be written using functions and arrays.
The program MUST and can ONLY contain the following function definitions and layout:
/**************************************************************
* This function takes in a filename and spells check it *
* @param file an input file stream (i.e. bonk.txt) *
***************************************************************/
void spell(string filename)
{
// function body
}
/**************************************************************
* This function takes a word from input file stream (i.e. *
* bonk.txt) and removes punctuation marks. Then, the function *
* updates the word in the caller argument (if necessary) *
* @param word a string that contains a word to be filtered *
**************************************************************/
void wordFilter(string& word)
{
// function body
}
/**************************************************************
* This function returns true if a word in input file stream *
* (i.e. bonk.txt) is in the dictionary, false otherwise. *
* @param word a string that contains a word to be checked *
* @return the Boolean status of the word (i.e. true or false) *
**************************************************************/
bool inDictionary(string word, string dictionary[])
{
// function body
}
/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main()
{
// Prompt the user to enter a file to be spell checked
spell(filename);
return 0;
}
**Please write clean code. Any code that does not meet the asking requirements will be rated 0 stars.
Explanation / Answer
#include<iostream>
using namespace std;
const string dictionary_filename = "dictionary.txt";
const int max_dictionary_words = 65000;
int dictionary_size = 0;
/**************************************************************
* This function takes in a filename and spells check it *
* @param file an input file stream (i.e. bonk.txt) *
***************************************************************/
void spell(string filename)
{
// declare an array of length max_dictionary_words. This is valid C++ these days!
string dictionary[ max_dictionary_words ];
// Read dictionary!
ifstream df( dicitonary_filename );
while( !df.empty() )
{
df >> dictionary[ dictionary_size++ ];
};
// Now spell check each word by first filtering and then checking
ifstream tf( filename );
while( !tf.empty() )
{
string word;
tf >> word;
wordFilter( &word );
if( !inDictionary( word, dictionary ) )
cout << word << " ";
};
df.close();
tf.close();
return;
}
/**************************************************************
* This function takes a word from input file stream (i.e. *
* bonk.txt) and removes punctuation marks. Then, the function *
* updates the word in the caller argument (if necessary) *
* @param word a string that contains a word to be filtered *
**************************************************************/
void wordFilter(string& word)
{
string filtered_word;
for( int i = 0; i < word.size(); i++ )
if( word[ i ] != ';'
|| word[ i ] != ','
|| word[ i ] != '.'
|| word[ i ] != ':' ) // add other checks as the case may be
filtered_word.append( 1, word[ i ] ); // add one copy of word[ i ]
word = filtered_word;
}
/**************************************************************
* This function returns true if a word in input file stream *
* (i.e. bonk.txt) is in the dictionary, false otherwise. *
* @param word a string that contains a word to be checked *
* @return the Boolean status of the word (i.e. true or false) *
**************************************************************/
bool inDictionary(string word, string dictionary[])
{
for( int i = 0; i < dictionary_size; i++ )
if( word == dictionary[ i ] )
return true;
return false;
}
/**************************************************************
* This is the main function of the program. *
* @return a value to terminate the program successfully *
**************************************************************/
int main()
{
string filename;
cout << "File to be spellchecked: ";
cin >> filename;
spell(filename);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.