mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-10 00:13:43 +00:00
30 lines
628 B
Go
30 lines
628 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 305.
|
|
//!+
|
|
|
|
// Package word provides utilities for word games.
|
|
package word
|
|
|
|
import "unicode"
|
|
|
|
// IsPalindrome reports whether s reads the same forward and backward.
|
|
// Letter case is ignored, as are non-letters.
|
|
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
|
|
}
|
|
|
|
//!-
|