Introduction
A spelling checker is a software tool designed to verify if words in a given text are spelled correctly.
It can be a useful tool in text editing and proofreading applications. In this tutorial, we will walk through
creating a basic spelling checker program in C++.
The primary goal of this program is to check each word in a text against a dictionary of valid words to
ensure that they are spelled correctly. If a word is not found in the dictionary, it will be flagged as potentially misspelled.
Objective
The objective of this program is to:
- Read an input text containing words to be checked.
- Compare each word with a predefined dictionary.
- Highlight misspelled words and suggest corrections.
C++ Spelling Checker Code
#include
#include
#include
#include
#include
#include
// Function to convert string to lowercase
std::string toLowerCase(const std::string &str) {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
// Function to load dictionary words into a set for fast lookup
void loadDictionary(std::unordered_set &dictionary, const std::string &dictionaryFile) {
std::ifstream file(dictionaryFile);
std::string word;
while (file >> word) {
dictionary.insert(toLowerCase(word));
}
}
// Function to check the spelling of a word
bool checkSpelling(const std::unordered_set &dictionary, const std::string &word) {
return dictionary.find(toLowerCase(word)) != dictionary.end();
}
int main() {
// Load dictionary of valid words
std::unordered_set dictionary;
loadDictionary(dictionary, "dictionary.txt");
// Input text from user
std::string inputText;
std::cout << "Enter text to check for spelling errors: "; std::getline(std::cin, inputText); std::stringstream ss(inputText); std::string word; // Process each word in the input text bool hasSpellingError = false; while (ss >> word) {
// Remove punctuation from the word
word.erase(remove_if(word.begin(), word.end(), ::ispunct), word.end());
// Check the spelling of the word
if (!checkSpelling(dictionary, word)) {
std::cout << "Misspelled word: " << word << std::endl;
hasSpellingError = true;
}
}
if (!hasSpellingError) {
std::cout << "No spelling errors found." << std::endl;
}
return 0;
}
Explanation of the Program
This C++ program works in the following manner:
- Dictionary Loading: The program loads a list of valid words (from a file named “dictionary.txt”)
into an unordered set. This makes it easy and fast to check if a word is correctly spelled. - Word Processing: The program processes each word from the input text, removing any punctuation to ensure
the words are checked properly. - Spell Checking: Each word is compared with the dictionary. If it’s not found in the dictionary,
it is flagged as a misspelled word. - Output: The program outputs the misspelled words, if any, and notifies the user about spelling errors.
How to Run the Program
Follow these steps to run the C++ spelling checker:
- Ensure that you have a C++ compiler (such as g++) installed on your machine.
- Create a text file called “dictionary.txt” with a list of valid words (one word per line).
- Save the above C++ code to a file with a “.cpp” extension (e.g., “SpellingChecker.cpp”).
- Compile the program using the following command:
g++ SpellingChecker.cpp -o SpellingChecker. - Run the program using the command:
./SpellingChecker(or “SpellingChecker.exe” on Windows). - Enter the text you wish to check, and the program will display any misspelled words.

