Created
August 25, 2014 07:21
-
-
Save elgs/c7fe4c3c83d8c3048f5e to your computer and use it in GitHub Desktop.
reverse string in Golang
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
func reverse(input string) string { | |
n := 0 | |
rune := make([]rune, len(input)) | |
for _, r := range input { | |
rune[n] = r | |
n++ | |
} | |
rune = rune[0:n] | |
// Reverse | |
for i := 0; i < n/2; i++ { | |
rune[i], rune[n-1-i] = rune[n-1-i], rune[i] | |
} | |
// Convert back to UTF-8. | |
output := string(rune) | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment