Skip to content
Snippets Groups Projects
Commit 7069faa2 authored by Martin Schmollinger's avatar Martin Schmollinger
Browse files

Initial solution to exercise gopalindrome

parent aef8cd1f
No related branches found
No related tags found
No related merge requests found
module github.com/turngeek/examples-go/src/gopalindrome
go 1.16
package main
import (
"fmt"
"os"
"strings"
"unicode"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gopalindrome [sentence]")
} else {
sentence := strings.Join(os.Args[1:], " ")
var result string
if isPalindrome := isPalindrome(sentence); isPalindrome {
result = "a"
} else {
result = "not a"
}
fmt.Printf("Your sentence: '%s' is %s palindrome!\n", sentence, result)
}
}
func isPalindrome(s string) bool {
var letters []rune
for _, r := range s {
if unicode.IsLetter(r) {
letters = append(letters, unicode.ToLower(r))
}
}
for i := range letters {
if letters[i] != letters[len(letters)-1-i] {
return false
}
}
return true
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment