MDGSF Software Engineer

[GO] reverse string

2018-01-17
GO

package main

import "fmt"

func main() {
    str := "abcdef"
    rstr := reverse(str)
    fmt.Println(rstr)
}

func reverse(str string) string {
    runes := []rune(str)
    l := 0
    r := len(runes) - 1
    for l <= r {
        runes[l], runes[r] = runes[r], runes[l]
        l++
        r--
    }
    return string(runes)
}
$ go run test.go
fedcba

weixingongzhonghao

Comments

Content