Skip to content

Commit 990b8ad

Browse files
Add solution for Challenge 2
1 parent f7e24d8 commit 990b8ad

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
)
8+
9+
func main() {
10+
// Read input from standard input
11+
scanner := bufio.NewScanner(os.Stdin)
12+
if scanner.Scan() {
13+
input := scanner.Text()
14+
15+
// Call the ReverseString function
16+
output := ReverseString(input)
17+
18+
// Print the result
19+
fmt.Println(output)
20+
}
21+
}
22+
23+
// ReverseString returns the reversed string of s.
24+
func ReverseString(s string) string {
25+
n := len(s)
26+
if n <= 1 || n > 1000 {
27+
return s
28+
}
29+
30+
b := []byte(s)
31+
for i, j := 0, n-1; i <j; i, j = i+1, j-1 {
32+
b[i], b[j] = b[j], b[i]
33+
}
34+
return string(b)
35+
}

0 commit comments

Comments
 (0)