Skip to content

Commit 70529c5

Browse files
authored
Merge pull request #15 from mick4711/main
Add solution for Challenge 2 by mick4711
2 parents c687b0a + 9fe9743 commit 70529c5

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
// check len contraint
26+
if len(s) >1000 {
27+
return "input string exceeds length limit (1000)"
28+
}
29+
30+
// convert to slice of runes
31+
r := []rune(s)
32+
l := len(r)
33+
34+
// swap in place
35+
for i :=0; i<l/2; i++ {
36+
r[i], r[l-i-1] = r[l-i-1], r[i]
37+
}
38+
39+
// // make a slice for the result
40+
// res := make([]rune, l)
41+
42+
// // fill result from the end backwards
43+
// for i, c := range r {
44+
// res[l-i-1] = c
45+
// }
46+
47+
return string(r)
48+
}

0 commit comments

Comments
 (0)