Palindrome Checker Program in Go

Published November 12, 2021

In this article, the palindrome checker program is created with Go programming language.

This is the steps to check if the sentence is a palindrome.

  1. Split the sample sentence into many characters.
  2. Create a reversed string from the splitted characters.
  3. Check the reversed and sample string. If the reversed is equals to sample string then it is a palindrome. Otherwise, it is not a palindrome.

This is the implementation in Go.

func isPalindrome(sample string) bool {
    // create a variable to store reversed string
    var reversed string

    // split sample string
    var words = strings.Split(sample, "")

    // insert all characters from the last index
    // into "reversed" variable
    for i := len(words) - 1; i >= 0; i-- {
        reversed += words[i]
    }

    // if sample is equals to reversed string
    // then the result is true
    if sample == reversed {
        return true
    }

    // return false if sample is not equals to reversed string
    return false
}

The isPalindrome() function is used in main function.

package main

import (
    "fmt"
    "strings"
)

func main() {
    // return true
    fmt.Println(isPalindrome("level"))

    // return false
    fmt.Println(isPalindrome("sample text"))
}

Output

true
false

I hope this article is helpful to learn about creating palindrome checker program in Go.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

876 Views