-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (70 loc) · 1.77 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
for {
fmt.Println("\nPalindrome Checker")
fmt.Println("1. What is a palindrome?")
fmt.Println("2. Check if a word is a palindrome")
fmt.Println("3. Exit")
fmt.Print("Choose an option: ")
choice, _ := reader.ReadString('\n')
choice = strings.TrimSpace(choice)
switch choice {
case "1":
fmt.Println("A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward.")
fmt.Println("For example: 'radar', 'level', '12321', and 'A man, a plan, a canal, Panama!' are palindromes.")
case "2":
fmt.Print("Enter a word: ")
word, _ := reader.ReadString('\n')
word = strings.TrimSpace(word)
if isPalindrome2(word) {
fmt.Printf("Yes, %s it's a palindrome!\n", word)
} else {
fmt.Printf("No, %s it's not a palindrome.\n", word)
}
case "3":
fmt.Println("Exiting...")
return
default:
fmt.Println("Invalid choice. Please choose again.")
}
}
}
func isPalindrome2(s string) bool {
if len(s) == 0 {
return false
}
for i := 0; i < len(s)/2; i++ {
if s[i] != s[len(s)-i-1] {
return false
}
}
return true
}
// invertText function is not used because isPalindrome2 directly compares characters for efficiency.
// Keeping it here for reference or potential future use.
/*
func invertText(text string) string {
var invertedText string
for i := len(text) - 1; i >= 0; i-- {
invertedText += string(text[i])
}
return invertedText
}
*/
// isPalindrome function is not used because isPalindrome2 provides better performance.
// Keeping it here for reference or potential future use.
/*
func isPalindrome(s string) bool {
if len(s) == 0 {
return false
}
return invertText(s) == s
}
*/