-
-
Notifications
You must be signed in to change notification settings - Fork 724
Add solution for Challenge 2 by 4m4x #780
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" | ||||||||||||
| "strings" | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| 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 { | ||||||||||||
| runes := []rune(s) | ||||||||||||
| output := []string{} | ||||||||||||
|
|
||||||||||||
| for i := len(runes) - 1; i > -1; i-- { | ||||||||||||
| output = append(output, string(s[i])) | ||||||||||||
|
Comment on lines
+29
to
+30
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. Critical bug: incorrect indexing breaks Unicode support. Line 30 uses For example, reversing Apply this diff to fix the bug: for i := len(runes) - 1; i > -1; i-- {
- output = append(output, string(s[i]))
+ output = append(output, string(runes[i]))
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| s = strings.Join(output, "") | ||||||||||||
| return s | ||||||||||||
| } | ||||||||||||
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 for scanner errors after the scan operation. While
scanner.Scan()returnsfalsefor both EOF and errors, you should checkscanner.Err()to handle actual I/O errors properly.Apply this diff to add 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