Skip to content

Instantly share code, notes, and snippets.

@Josephchinedu
Created August 7, 2022 21:59
Show Gist options
  • Save Josephchinedu/837f567605787963854d1712687e9943 to your computer and use it in GitHub Desktop.
Save Josephchinedu/837f567605787963854d1712687e9943 to your computer and use it in GitHub Desktop.
reverse string in golang
package main
import "fmt"
func main() {
input := "Joseph Chinedu Ogbu"
rev := Reverse(input)
doubleRev := Reverse(rev)
fmt.Printf("original: %q\n", input)
fmt.Printf("reversed: %q\n", rev)
fmt.Printf("double reversed: %q\n", doubleRev)
}
func Reverse(input string) string {
r := []rune(input)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment