Go Program to Check if a String is a Palindrome

Introduction

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). This guide will show you how to write a Go program that checks whether a given string is a palindrome.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a string.
  • Checks whether the string is a palindrome.
  • Displays the result.

Example:

  • Input: madam
  • Output: madam is a palindrome

Solution Steps

  1. Import the fmt and strings Packages: Use import "fmt" and import "strings" to include the fmt package for formatted I/O operations and strings package for string manipulation.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Input the String: Use fmt.Scanln to take input from the user for the string.
  4. Normalize the String: Convert the string to lowercase and remove any spaces or punctuation.
  5. Check if the String is a Palindrome: Reverse the string and compare it to the original string.
  6. Display the Result: Use fmt.Println to display whether the string is a palindrome.

Go Program

package main

import (
    "fmt"
    "strings"
)

/**
 * Go Program to Check if a String is a Palindrome
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the input string
    var input string

    // Step 2: Prompt the user to enter a string
    fmt.Print("Enter a string: ")
    fmt.Scanln(&input)

    // Step 3: Normalize the string by converting it to lowercase and removing spaces
    normalized := strings.ToLower(strings.ReplaceAll(input, " ", ""))

    // Step 4: Check if the string is a palindrome
    if isPalindrome(normalized) {
        fmt.Println(input, "is a palindrome")
    } else {
        fmt.Println(input, "is not a palindrome")
    }
}

// Function to check if a string is a palindrome
func isPalindrome(s string) bool {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        if runes[i] != runes[j] {
            return false
        }
    }
    return true
}

Explanation

Step 1: Declare Variables

  • The variable input is declared to store the user’s input string.

Step 2: Input the String

  • The program prompts the user to enter a string using fmt.Print. The fmt.Scanln function reads the input and stores it in the input variable.

Step 3: Normalize the String

  • The program converts the string to lowercase using strings.ToLower to make the palindrome check case-insensitive. It also removes spaces using strings.ReplaceAll to handle phrases correctly.

Step 4: Check if the String is a Palindrome

  • The program uses the isPalindrome function to check if the string reads the same forward and backward. This function converts the string to a slice of runes, then compares characters from the start and end, moving towards the center.

Step 5: Display the Result

  • The program prints whether the original input string is a palindrome based on the result of the isPalindrome function.

Palindrome Function

  • The isPalindrome function iterates over the slice of runes, checking if the characters at the beginning and end are the same. If all characters match, the string is a palindrome; otherwise, it is not.

Output Example

Example 1:

Enter a string: madam
madam is a palindrome

Example 2:

Enter a string: hello
hello is not a palindrome

Example 3:

Enter a string: A man a plan a canal Panama
A man a plan a canal Panama is a palindrome

Conclusion

This Go program demonstrates how to check whether a string is a palindrome. It covers basic programming concepts such as string manipulation, loops, and conditional statements. This example is useful for beginners learning Go programming and understanding how to work with strings and perform palindrome checks.

Leave a Comment

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

Scroll to Top