Created
August 7, 2022 21:59
-
-
Save Josephchinedu/837f567605787963854d1712687e9943 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
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