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
35 changes: 35 additions & 0 deletions challenge-2/submissions/fzzv/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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)
}
}
Comment on lines +9 to +21
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

Add error handling for scanner.

The code doesn't check scanner.Err() after the scan operation. If an I/O error occurs, it will be silently ignored.

Apply this diff to add proper error handling:

 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)
 	}
+	if err := scanner.Err(); err != nil {
+		fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
+		os.Exit(1)
+	}
 }
📝 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() {
// 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)
}
}
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)
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
os.Exit(1)
}
}
🤖 Prompt for AI Agents
In challenge-2/submissions/fzzv/solution-template.go around lines 9 to 21, the
main function reads input with bufio.NewScanner but does not check scanner.Err()
after scanning; add a check immediately after the Scan block to call
scanner.Err(), and if non-nil, write the error to stderr (or log it) and exit
with a non-zero status so I/O errors are not silently ignored; keep the existing
successful path that prints the reversed string when no error occurred.


// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
n := len(s)
if n <= 1 || n > 1000 {
return s
}

b := []byte(s)
for i, j := 0, n-1; i <j; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
return string(b)
}
Comment on lines +24 to +35
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

Byte-level reversal breaks multi-byte UTF-8 characters.

The function reverses the string at the byte level, which corrupts multi-byte UTF-8 characters like emojis or non-ASCII text. For example, "Hello 👋" would become garbled instead of "👋 olleH".

Apply this diff to handle Unicode correctly using runes:

 // ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-    n := len(s)
-    if n <= 1 || n > 1000 {
-        return s
-    }
-    
-    b := []byte(s)
-    for i, j := 0, n-1; i <j; i, j = i+1, j-1 {
-        b[i], b[j] = b[j], b[i]
-    }
-    return string(b)
+	runes := []rune(s)
+	n := len(runes)
+	if n <= 1 || n > 1000 {
+		return s
+	}
+	
+	for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
+		runes[i], runes[j] = runes[j], runes[i]
+	}
+	return string(runes)
 }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In challenge-2/submissions/fzzv/solution-template.go around lines 24 to 35, the
function reverses the string at the byte level which corrupts multi-byte UTF-8
characters; change to operate on runes by converting the input to a []rune, use
its length for the n and the 1000 check, reverse the rune slice in-place (swap
runes[i] and runes[j] while i<j), and return string(runes) so multi-byte Unicode
characters (emojis, non-ASCII) are preserved.

Loading