Skip to content
Merged
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
50 changes: 50 additions & 0 deletions challenge-17/submissions/hudazaan/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"fmt"
"strings"
"unicode"
)

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.")
}
}
Comment on lines +9 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Use bufio.Scanner to read multi-word input.

fmt.Scanln stops reading at the first whitespace, which breaks palindrome checking for common test cases like "race car" or "A man a plan a canal Panama". Palindrome checkers typically need to handle phrases with spaces.

Apply this diff to fix the input reading:

 import (
+	"bufio"
 	"fmt"
+	"os"
 	"strings"
 	"unicode"
 )

 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)
+	scanner := bufio.NewScanner(os.Stdin)
+	scanner.Scan()
+	input := scanner.Text()

 	// Call the IsPalindrome function and print the result
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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.")
}
}
import (
"bufio"
"fmt"
"os"
"strings"
"unicode"
)
func main() {
// Get input from the user
fmt.Print("Enter a string to check if it's a palindrome: ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
input := scanner.Text()
// 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.")
}
}
🤖 Prompt for AI Agents
In challenge-17/submissions/hudazaan/solution-template.go around lines 9 to 22,
replace the fmt.Scanln input reading with a bufio.Scanner (or bufio.NewReader)
to capture the full line including spaces: create a scanner for os.Stdin, call
scanner.Scan(), assign input = scanner.Text(), and handle scanner.Err() if
present. Also ensure bufio and os are imported (add them to the import block if
missing). This will allow multi-word phrases like "race car" to be read
correctly for palindrome checking.


// 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 {
// Clean the string: remove non-alphanumeric characters and convert to lowercase
cleaned := strings.Builder{}
for _, char := range s {
if unicode.IsLetter(char) || unicode.IsDigit(char) {
cleaned.WriteRune(unicode.ToLower(char))
}
}

cleanStr := cleaned.String()

// Check if the cleaned string is the same forwards and backwards
left := 0
right := len(cleanStr) - 1

for left < right {
if cleanStr[left] != cleanStr[right] {
return false
}
left++
right--
}

return true
}
Loading