Golang
Golang

 

 

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). Examples of palindromes include “madam”, “racecar”, and “level”. This program is designed to check if a given string is a palindrome in Go programming language.

Objective

The main objective of this program is to check if a given string is a palindrome. It will read the string input, process it by ignoring spaces and case, and then determine whether it can be reversed to match the original string.

Go Code for Palindrome Check

package main

import (
    "fmt"
    "strings"
)

// Function to check if the string is a palindrome
func isPalindrome(s string) bool {
    // Remove spaces and convert to lowercase
    s = strings.ToLower(strings.ReplaceAll(s, " ", ""))

    // Loop through the string and check characters from both ends
    for i := 0; i < len(s)/2; i++ {
        if s[i] != s[len(s)-i-1] {
            return false
        }
    }
    return true
}

func main() {
    // Take input from the user
    var input string
    fmt.Print("Enter a string to check if it is a palindrome: ")
    fmt.Scanln(&input)

    // Check if the input string is a palindrome
    if isPalindrome(input) {
        fmt.Println("The string is a palindrome.")
    } else {
        fmt.Println("The string is NOT a palindrome.")
    }
}

Explanation of the Program

The program works by performing the following steps:

  1. Input Handling: The program first prompts the user to input a string that they wish to check for being a palindrome.
  2. Preprocessing: The isPalindrome function processes the input string by removing any spaces and converting all characters to lowercase. This is to ensure the comparison is case-insensitive and ignores any spaces.
  3. Palindrome Check: The program then compares characters from the start and end of the string. If any pair of characters don’t match, the function returns false, indicating that the string is not a palindrome. If all pairs match, it returns true.
  4. Output: Based on the result from the isPalindrome function, the program prints whether the string is a palindrome or not.

How to Run the Program

  1. First, ensure that you have Go installed on your system. You can download it from here.
  2. Save the above code into a file named palindrome.go.
  3. Open a terminal and navigate to the directory where the palindrome.go file is saved.
  4. Run the following command to compile and execute the program:
    go run palindrome.go
  5. After running the program, it will prompt you to enter a string. Type the string you want to check and press Enter.
  6. The program will output whether the string is a palindrome or not.

 

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