Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mimiyanwk committed Dec 19, 2018
0 parents commit e9bfdeb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module github.com/mimiyanwk/stringutil
9 changes: 9 additions & 0 deletions reverse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package stringutil

func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
21 changes: 21 additions & 0 deletions reverse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package stringutil

import (
"testing"
)

func TestReverse(t *testing.T) {
cases := []struct {
in, want string
}{
{"Hello, world", "dlrow ,olleH"},
{"Hello, 世界", "界世 ,olleH"},
{"", ""},
}
for _, c := range cases {
got := Reverse(c.in)
if got != c.want {
t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
}
}
}

0 comments on commit e9bfdeb

Please sign in to comment.