Skip to content

Commit 3319dad

Browse files
authored
Add solution for Challenge 17 by JoQCorreia (#1895)
Auto-merged after 2 days with all checks passing. PR: #1895 Author: @JoQCorreia
1 parent d089e35 commit 3319dad

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
"unicode"
7+
)
8+
9+
func main() {
10+
// Get input from the user
11+
var input string
12+
fmt.Print("Enter a string to check if it's a palindrome: ")
13+
fmt.Scanln(&input)
14+
15+
// Call the IsPalindrome function and print the result
16+
result := IsPalindrome(input)
17+
if result {
18+
fmt.Println("The string is a palindrome.")
19+
} else {
20+
fmt.Println("The string is not a palindrome.")
21+
}
22+
}
23+
24+
// IsPalindrome checks if a string is a palindrome.
25+
// A palindrome reads the same backward as forward, ignoring case, spaces, and punctuation.
26+
func IsPalindrome(s string) bool {
27+
28+
//Removes all characters that are not letters or numbers and makes string lowercase
29+
pal := strings.Map(func(c rune) rune {
30+
if !unicode.IsLetter(c) && !unicode.IsNumber(c) {
31+
return -1
32+
}
33+
return c
34+
}, strings.ToLower(s))
35+
36+
//If string has no characters, it returns true
37+
if len(pal) == 0 {
38+
return true
39+
}
40+
41+
// Convert to runes to safely handle multi-byte Unicode characters
42+
palRunes := []rune(pal)
43+
indx := len(palRunes) - 1
44+
45+
for i := 0; i <= (indx - i); i++ {
46+
//Loop compares the letters on opposite sides of the string for matches
47+
if palRunes[i] != palRunes[indx-i] {
48+
return false
49+
}
50+
51+
continue
52+
53+
}
54+
55+
return true
56+
}

0 commit comments

Comments
 (0)