A palindrome reads the same forwards and backwards.
What to build
A program that tells whether each word is a palindrome, ignoring upper and lower case.
Requirements
- Read n, then n words made of letters.
- For each word print
yesif it is a palindrome andnoif not. Upper and lower case letters count as equal.
Input
Line 1: n. Then n words.
Output
n lines.
Constraints
1 ≤ n ≤ 50; words have 1 to 30 English letters.
Example
Input:
3
level Noon hello
Output:
yes
yes
no
Why it works
After converting to lower case, a word is a palindrome when each letter equals the one at the mirrored position. Only the first half has to be compared.
Examples
Example 1 · Input
3 level Noon hello
Expected output
yes yes no
Example 2 · Input
2 a Ab
Expected output
yes no
Hints
Hint 1
Convert the word to lower case with strings.ToLower first.
Hint 2
Compare the letter at position i with the one at len(w)-1-i.
Solution
Show one correct solution (try it yourself first)
package main
import (
"fmt"
"strings"
)
func main() {
var n int
fmt.Scan(&n)
for i := 0; i < n; i++ {
var w string
fmt.Scan(&w)
w = strings.ToLower(w)
ok := true
for a := 0; a < len(w)/2; a++ {
if w[a] != w[len(w)-1-a] {
ok = false
break
}
}
if ok {
fmt.Println("yes")
} else {
fmt.Println("no")
}
}
}