Created
November 29, 2020 18:41
-
-
Save 0xMarK/734767ad5385581bad55eb2688db5d40 to your computer and use it in GitHub Desktop.
reverseString
This file contains hidden or 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
func reverseStringRecursive(_ string: String) -> String { | |
guard !string.isEmpty else { return string } | |
return reverseStringRecursive(String(string.suffix(string.count - 1))) + String(string[string.index(string.startIndex, offsetBy: 0)]) | |
} | |
func reverseStringIterative(_ string: String) -> String { | |
var result = "" | |
for i in (0..<string.count).reversed() { | |
result += String(string[string.index(string.startIndex, offsetBy: i)]) | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment