Spelling Checker in Go Programming Language

 

In this tutorial, we’ll explore how to create a simple spelling checker using Go programming language. The program will analyze a given text and identify any misspelled words.

Objective

The objective of this program is to check if the words in a provided text are spelled correctly by comparing them against a dictionary of valid words. If any word is misspelled, the program will highlight it as an error.

Code Implementation


package main

import (
    "bufio"
    "fmt"
    "os"
    "strings"
)

// Predefined dictionary of valid words
var dictionary = map[string]bool{
    "hello": true,
    "world": true,
    "go":    true,
    "programming": true,
    "language": true,
    "is":    true,
    "fun":   true,
}

// Function to check spelling of words in text
func checkSpelling(text string) {
    words := strings.Fields(text)
    for _, word := range words {
        // Remove any punctuation from the word
        cleanedWord := strings.ToLower(strings.Trim(word, ",.!?"))
        if !dictionary[cleanedWord] {
            fmt.Printf("Misspelled word: %s\n", word)
        }
    }
}

func main() {
    // Prompt user for input
    fmt.Println("Enter text to check spelling:")
    scanner := bufio.NewScanner(os.Stdin)
    scanner.Scan()
    text := scanner.Text()

    // Check spelling of the entered text
    checkSpelling(text)
}
            

Program Explanation

This Go program checks the spelling of words in a given text. Here’s a breakdown of the code:

  • Dictionary: A map containing predefined valid words used to compare with the words in the input text.
  • checkSpelling function: This function takes a text input, splits it into individual words, and checks each word against the dictionary.
  • Text Input: The program prompts the user to enter a text, which is then processed by the checkSpelling function.
  • Misspelled Word Detection: If any word is not found in the dictionary, it is flagged as a misspelled word and printed to the console.

How to Run the Program:

  1. Ensure you have Go installed on your system. You can download it from here.
  2. Save the code into a file named spelling_checker.go.
  3. Open a terminal or command prompt and navigate to the directory where the file is saved.
  4. Run the program using the command: go run spelling_checker.go.
  5. Enter a text to check for spelling errors.
© 2025 Learn Programming. All Rights Reserved.

 

Leave a Reply

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