Last active
May 26, 2018 10:06
-
-
Save itang/8109481 to your computer and use it in GitHub Desktop.
golang: reverse string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"unicode/utf8" | |
) | |
func main() { | |
fmt.Println(reverse("abc")) | |
fmt.Println(reverse("中文")) | |
} | |
func reverse(s string) string { | |
cs := make([]rune, utf8.RuneCountInString(s)) | |
i := len(cs) | |
for _, c := range s { | |
i-- | |
cs[i] = c | |
} | |
return string(cs) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go