-
-
Notifications
You must be signed in to change notification settings - Fork 711
Add solution for Challenge 2 by fzzv #635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) | ||
| } | ||
| } | ||
|
|
||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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)
}
🤖 Prompt for AI Agents |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
🤖 Prompt for AI Agents