-
-
Notifications
You must be signed in to change notification settings - Fork 709
Add solution for Challenge 2 by labib99 #619
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,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 { | ||||||||||
| // TODO: Implement the function | ||||||||||
|
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. Remove the TODO comment. The function is already implemented, so the TODO comment on line 25 should be removed. Apply this diff: // ReverseString returns the reversed string of s.
func ReverseString(s string) string {
- // TODO: Implement the function
runes := []rune(s)📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||
| runes := []rune(s) | ||||||||||
| tmpRunes := make([]rune, len(runes)) | ||||||||||
| for idx, r := range runes { | ||||||||||
| reverseIdx := len(runes) - 1 - idx | ||||||||||
| tmpRunes[reverseIdx] = r | ||||||||||
| } | ||||||||||
| return string(tmpRunes) | ||||||||||
| } | ||||||||||
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 scanner might encounter errors during input reading that should be checked. After the
if scanner.Scan()block, you should verifyscanner.Err()to ensure no I/O errors occurred.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