I am creating a spell checker that uses a dictionary in a linked list. I also have a book fin another that I am reading word by word.
I want to create a loop that will check the first word in the book against all the words in the linked list(dictionary). If the word is in the dictionary I will stop and count the word the correct, then move on to the next word in the book and read it against all words in the linked list and so on. If I get all the way through the linked list, the word will be counted as incorrect and we will do the same as earlier, get a new word from the book and check it against all the words in the linked list.
How can I do this? I can't find a simple way to iterate through a linked list while checking it against another value.
Read the words in the dictionary file into a list (std::list), if you must use a linked list.
For each word read in from the book file, spell check with:
#include <list>
#include <string>
#include <algorithm>
// return true if the word is in the dictionary (use a loop)
bool spell_check( const std::list<std::string>& dictionary, const std::string& word ) {
// for each string in the dictionary
for( constauto& str : dictionary )
if( word == str ) returntrue ; // the word matched this string in the dictionary, return true
returnfalse ; // reached the end without finding the word
}
// alternative: return true if the word is in the dictionary (use an algorithm)
bool spell_check2( const std::list<std::string>& dictionary, const std::string& word ) {
// use the algorithm std::find https://en.cppreference.com/w/cpp/algorithm/findreturn std::find( dictionary.begin(), dictionary.end(), word ) != dictionary.end() ;
}
Note: adjust case, strip punctuation etc. for the words.