Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions challenge-17/submissions/shansing/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"regexp"
"strings"
)

func main() {
// Get input from the user
var input string
fmt.Print("Enter a string to check if it's a palindrome: ")
fmt.Scanln(&input)

// Call the IsPalindrome function and print the result
result := IsPalindrome(input)
if result {
fmt.Println("The string is a palindrome.")
} else {
fmt.Println("The string is not a palindrome.")
}
}

// IsPalindrome checks if a string is a palindrome.
// A palindrome reads the same backward as forward, ignoring case, spaces, and punctuation.
func IsPalindrome(s string) bool {
if len(s) <= 0 {
return true
}
// 1. Clean the string (remove spaces, punctuation, and convert to lowercase)
cleaned := regexp.MustCompile("[^a-z0-9]+").ReplaceAllString(strings.ToLower(s), "")
// 2. Check if the cleaned string is the same forwards and backwards
for i, j := 0, len(cleaned)-1; i < j; i, j = i+1, j-1 {
if cleaned[i] != cleaned[j] {
return false
}
}
return true
}
Loading