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
- Import the fmt and strings Packages: Use import "fmt"andimport "strings"to include the fmt package for formatted I/O operations and strings package for string manipulation.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Input the String: Use fmt.Scanlnto take input from the user for the string.
- Normalize the String: Convert the string to lowercase and remove any spaces or punctuation.
- Check if the String is a Palindrome: Reverse the string and compare it to the original string.
- Display the Result: Use fmt.Printlnto 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 inputis 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. Thefmt.Scanlnfunction reads the input and stores it in theinputvariable.
Step 3: Normalize the String
- The program converts the string to lowercase using strings.ToLowerto make the palindrome check case-insensitive. It also removes spaces usingstrings.ReplaceAllto handle phrases correctly.
Step 4: Check if the String is a Palindrome
- The program uses the isPalindromefunction 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 isPalindromefunction.
Palindrome Function
- The isPalindromefunction 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.