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:

  1. Ensure that you have a C++ compiler (such as g++) installed on your machine.
  2. Create a text file called “dictionary.txt” with a list of valid words (one word per line).
  3. Save the above C++ code to a file with a “.cpp” extension (e.g., “SpellingChecker.cpp”).
  4. Compile the program using the following command: g++ SpellingChecker.cpp -o SpellingChecker.
  5. Run the program using the command: ./SpellingChecker (or “SpellingChecker.exe” on Windows).
  6. Enter the text you wish to check, and the program will display any misspelled words.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)