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

import (
"bufio"
"fmt"
"os"
)

func main() {
// Read input from standard input
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
input := scanner.Text()

// Call the ReverseString function
output := ReverseString(input)

// Print the result
fmt.Println(output)
}
}

// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
l := len(s)
a := make([]byte, l, l)

for i := len(s) - 1; i >= 0; i-- {
a[l-1-i] = s[i]
}

return string(a)
}
Comment on lines +24 to +33
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 | 🔴 Critical

Handle UTF-8 runes, not raw bytes.

Reversing by byte corrupts any multi-byte characters (e.g., "こんにちは" becomes invalid UTF-8). Convert to runes before reversing and rebuild the string.

Apply this diff:

-func ReverseString(s string) string {
-	l := len(s)
-	a := make([]byte, l, l)
-
-	for i := len(s) - 1; i >= 0; i-- {
-		a[l-1-i] = s[i]
-	}
-
-	return string(a)
-}
+func ReverseString(s string) string {
+	runes := []rune(s)
+	for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
+		runes[i], runes[j] = runes[j], runes[i]
+	}
+	return string(runes)
+}
🤖 Prompt for AI Agents
In challenge-2/submissions/macborowy/solution-template.go around lines 24 to 33,
the function reverses raw bytes which corrupts multi-byte UTF-8 characters;
convert the input string to a slice of runes, reverse that rune slice (swap rune
positions from ends toward center) and then return a new string from the
reversed rune slice so multi-byte characters are preserved.