Created
September 28, 2017 13:59
-
-
Save arehmandev/1f5b2fccf36fc740e10377743468bcd4 to your computer and use it in GitHub Desktop.
Reverse a string (2 different ways) in go
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" | |
"strings" | |
) | |
var ( | |
word = "test" | |
reversedstringarray []string | |
reversedstring string | |
) | |
func main() { | |
reverse(word) | |
reverse2(word) | |
} | |
func reverse(word string) { | |
stringfinalindex := len(word) - 1 | |
stringarray := strings.Split(word, "") | |
for i := range stringarray { | |
reversedstringarray = append(reversedstringarray, stringarray[stringfinalindex-i]) | |
reversedstring = strings.Join(reversedstringarray, "") | |
} | |
fmt.Println(reversedstring) | |
} | |
func reverse2(word string) { | |
stringfinalindex := len(word) - 1 | |
stringarray := strings.Split(word, "") | |
for i := stringfinalindex; i < 0; i-- { | |
reversedstringarray = append(reversedstringarray, stringarray[i]) | |
} | |
fmt.Println(reversedstring) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment