Introduction

A Spelling Checker is a program that automatically detects and identifies words in a given text that are potentially misspelled. The goal of this project is to develop a simple Spelling Checker in C programming language that will check whether the words in a text are spelled correctly. This checker will compare the words in the text with a predefined dictionary of correctly spelled words.

Objective

The primary objective of this program is to provide a simple way of detecting misspelled words in a text. This can be useful in text processing, document editing, and various applications where accurate spelling is important. The program will display the incorrect words and suggest possible corrections based on a dictionary file.

Program Code

#include 
#include 
#include 

#define MAX_WORD_LENGTH 100
#define DICTIONARY_SIZE 1000

// Function to load dictionary words into an array
int loadDictionary(char dictionary[][MAX_WORD_LENGTH]) {
    FILE *file = fopen("dictionary.txt", "r");
    if (file == NULL) {
        printf("Error: Dictionary file not found!\n");
        return 0;
    }

    int i = 0;
    while (fscanf(file, "%s", dictionary[i]) != EOF && i < DICTIONARY_SIZE) {
        i++;
    }
    fclose(file);
    return i;
}

// Function to check if a word exists in the dictionary
int isWordInDictionary(char word[], char dictionary[][MAX_WORD_LENGTH], int dictionarySize) {
    for (int i = 0; i < dictionarySize; i++) { if (strcmp(word, dictionary[i]) == 0) { return 1; // Word found in dictionary } } return 0; // Word not found in dictionary } // Function to check spelling in the input text void checkSpelling(char text[], char dictionary[][MAX_WORD_LENGTH], int dictionarySize) { char word[MAX_WORD_LENGTH]; int wordIndex = 0; for (int i = 0; text[i] != '\0'; i++) { if (isalpha(text[i])) { word[wordIndex++] = text[i]; } else { if (wordIndex > 0) {
                word[wordIndex] = '\0';
                if (!isWordInDictionary(word, dictionary, dictionarySize)) {
                    printf("Misspelled word: %s\n", word);
                }
                wordIndex = 0;
            }
        }
    }

    // Check for the last word
    if (wordIndex > 0) {
        word[wordIndex] = '\0';
        if (!isWordInDictionary(word, dictionary, dictionarySize)) {
            printf("Misspelled word: %s\n", word);
        }
    }
}

int main() {
    char dictionary[DICTIONARY_SIZE][MAX_WORD_LENGTH];
    char text[1000];

    printf("Enter the text for spelling check:\n");
    fgets(text, sizeof(text), stdin);

    int dictionarySize = loadDictionary(dictionary);
    if (dictionarySize == 0) {
        return 1;  // Dictionary loading failed
    }

    checkSpelling(text, dictionary, dictionarySize);

    return 0;
}

Explanation of the Program

The program works in three main parts:

  • Load Dictionary: The loadDictionary function reads a list of correctly spelled words from a file called dictionary.txt</>. These words are stored in a 2D array for fast comparison.
  • Word Checking: The checkSpelling function scans the input text for words and checks if each word exists in the dictionary. If a word is not found in the dictionary, it is flagged as misspelled.
  • Main Function: The main function handles user input (the text to be checked), loads the dictionary, and invokes the function to check for misspelled words.

How to Run the Program

To run the Spelling Checker program:

  1. Save the C code in a file, for example spell_checker.c.
  2. Create a text file named dictionary.txt with a list of correctly spelled words, one word per line.
  3. Compile the program using a C compiler. For example, using GCC: gcc spell_checker.c -o spell_checker.
  4. Run the compiled program: ./spell_checker.
  5. Enter the text you want 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 :)