Skip to content

Commit 5aeb501

Browse files
committed
Solution for reverse a string
1 parent e178b1d commit 5aeb501

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Text/reverse.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"bufio"
7+
"unicode/utf8"
8+
"strings"
9+
)
10+
11+
func main() {
12+
reader := bufio.NewReader(os.Stdin)
13+
for {
14+
line, err := reader.ReadString('\n')
15+
16+
if err != nil {
17+
break
18+
}
19+
line = strings.TrimRight(line, "\r\n")
20+
fmt.Println(reverseString(line));
21+
}
22+
}
23+
24+
func reverseString(source string) string {
25+
ret := make([]rune, len(source))
26+
i := len(source)
27+
28+
for _, c := range source {
29+
if c != utf8.RuneError {
30+
i--
31+
ret[i] = c
32+
}
33+
}
34+
35+
return string(ret[i:])
36+
}

0 commit comments

Comments
 (0)