给定一个字符串,任务是反转字符串。这里有一些例子。
方法 1:通过交换字母来反转字符串,例如第一个与最后一个,第二个与第二个最后一个,依此类推。
例子:
// Golang program to reverse a string
package main
// importing fmt
import "fmt"
// function, which takes a string as
// argument and return the reverse of string.
func reverse(s string) string {
rns := []rune(s) // convert to rune
for i, j := 0, len(rns)-1; i < j; i, j = i+1, j-1 {
// swap the letters of the string,
// like first with last and so on.
rns[i], rns[j] = rns[j], rns[i]
}
// return the reversed string.
return string(rns)
}
func main() {
// Reversing the string.
str := "Geeks"
// returns the reversed string.
strRev := reverse(str)
fmt.Println(str)
fmt.Println(strRev)
}
输出:
Geeks
skeeG
方法 2:本示例声明一个空字符串,然后从末尾开始逐个追加字符。
例子:
// Golang program to reverse a string
package main
// importing fmt
import "fmt"
// function, which takes a string as
// argument and return the reverse of string.
func reverse(str string) (result string) {
for _, v := range str {
result = string(v) + result
}
return
}
func main() {
// Reversing the string.
str := "Geeks"
// returns the reversed string.
strRev := reverse(str)
fmt.Println(str)
fmt.Println(strRev)
}
输出:
Geeks
skeeG