Created
January 19, 2020 20:28
-
-
Save mathonsunday/111748368469ab3acff0bd77bef923c8 to your computer and use it in GitHub Desktop.
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 reverse(_ str: inout String) { | |
if str.isEmpty { | |
return | |
} | |
var leftPointer = str.startIndex | |
var rightPointer = str.index(before: str.endIndex) | |
while leftPointer < rightPointer { | |
let leftChar = str[leftPointer] | |
str.replaceSubrange(leftPointer..<str.index(after: leftPointer), with: String(str[rightPointer])) | |
str.replaceSubrange(rightPointer..<str.index(after: rightPointer), with: String(leftChar)) | |
if leftPointer < str.endIndex { | |
leftPointer = str.index(after: leftPointer) | |
} | |
if rightPointer > str.startIndex { | |
rightPointer = str.index(before: rightPointer) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// NICE WORK!
// I made it a String extension with a mutating func