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
30 changes: 30 additions & 0 deletions challenge-2/submissions/DaniilYuz/solution-template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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 scanning. If an I/O error occurs while reading from stdin, it will be silently ignored, which could lead to unexpected behavior or silent failures.

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/DaniilYuz/solution-template.go around lines 9 to 21,
the scanner error is not checked after scanning which can silently ignore I/O
problems; after the Scan() block call scanner.Err(), and if it returns a non-nil
error write a descriptive message to stderr (using fmt.Fprintln(os.Stderr, ...))
and exit with a non-zero status (os.Exit(1)) or return, otherwise continue to
call ReverseString and print the output; ensure the error check runs after the
Scan() completes so genuine read errors are handled.


// ReverseString returns the reversed string of s.
func ReverseString(s string) string {
res := []byte(s)
for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
res[i], res[j] = res[j], res[i]
}
return string(res)
}
Comment on lines +24 to +30
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

Fix Unicode handling in ReverseString.

The function uses []byte(s) which reverses individual bytes. This breaks multi-byte UTF-8 characters like emojis, Chinese/Arabic/Cyrillic text, etc. For example, reversing "Hello 世界" would corrupt the Chinese characters since each character spans multiple bytes.

Apply this diff to handle Unicode correctly:

 // ReverseString returns the reversed string of s.
 func ReverseString(s string) string {
-	res := []byte(s)
+	res := []rune(s)
 	for i, j := 0, len(res)-1; i < j; i, j = i+1, j-1 {
 	    res[i], res[j] = res[j], res[i] 
 	}
 	return string(res)
 }
🤖 Prompt for AI Agents
In challenge-2/submissions/DaniilYuz/solution-template.go around lines 24 to 30,
the function reverses bytes using []byte(s) which corrupts multi-byte UTF-8
characters; convert the string to a []rune, reverse the rune slice in-place
(swap runes from ends moving inward), and return string(runes) so Unicode
characters are preserved while keeping the same function signature and behavior.

Loading