-
-
Notifications
You must be signed in to change notification settings - Fork 709
Add solution for Challenge 2 by iamsurajmandal #715
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,32 @@ | ||
| 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 { | ||
| runeString := []rune(s) | ||
|
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. Fix indentation and remove unnecessary semicolon. Lines 25 and 27 use spaces for indentation instead of tabs, and line 27 has an unnecessary semicolon. Go convention is to use tabs for indentation and avoid semicolons at the end of statements. Run gofmt -w challenge-2/submissions/iamsurajmandal/solution-template.goAlso applies to: 27-27 🤖 Prompt for AI Agents |
||
| for i, j := 0, len(runeString) - 1; i < j; i, j = i + 1, j - 1 { | ||
| temp := runeString[i]; | ||
| runeString[i] = runeString[j] | ||
| runeString[j] = temp | ||
| } | ||
| return string(runeString) | ||
| } | ||
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 reading input. If
scanner.Scan()returnsfalse, it could be due to EOF (expected) or an error (unexpected).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