Skip to content

Instantly share code, notes, and snippets.

@johnbuhay
Created November 23, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbuhay/d0dbed1a31a4e3067e018838cb124a2c to your computer and use it in GitHub Desktop.
Save johnbuhay/d0dbed1a31a4e3067e018838cb124a2c to your computer and use it in GitHub Desktop.
IsPalindrome
// https://play.golang.org/p/gHUzTTE-IUy
// write a function that prints either True of False
// # depending on whether its only argument, a string, is a
// # palindrome
// #
// # racecar == racecar : True
// # johnbuhay == yahubnhoj : False
// # tacocat == tacocat : True
// # noon == noon : True
package main
import (
"fmt"
)
var words = []string{"racecar", "johnbuhay", "noon", "tacocat"}
func main() {
for _, word := range words {
fmt.Println(IsPalindrome(word))
}
}
// IsPalindrome returns true if input is a palindrome
func IsPalindrome(s string) bool {
// compare the last item to first item, return false when they dont match
// return true, when you have reached the middle
length := len(s)
for i := range s {
if i == length/2 {
return true
}
if string(s[i]) != string(s[(length-1)-i]) {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment