Created
November 23, 2020 18:38
-
-
Save johnbuhay/d0dbed1a31a4e3067e018838cb124a2c to your computer and use it in GitHub Desktop.
IsPalindrome
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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