Golang

 

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.

 

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 :)