Reverse a string in Go
Reverses by runes rather than bytes, which is the only way that works for non-ASCII.
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
Keywords and builtins used here
forfunclenreturnrunestring
The run, in numbers
- Lines
- 7
- Characters to type
- 169
- Tokens
- 72
- Three-star pace
- 95 tpm
At the three-star pace of 95 tokens a minute, this run takes about 45 seconds.
Step 5 of 5 in Strings & formatting, step 29 of 30 in Language basics.