Created
February 11, 2019 01:44
-
-
Save lawreyios/36776a6e818b520e26e5655cc7cc323d to your computer and use it in GitHub Desktop.
Palindrome using String Index (Swift)
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 isPalindrome(inputString: String) -> Bool { | |
| let stringLength = inputString.count | |
| var position = 0 | |
| while position < stringLength / 2 { | |
| let startIndex = inputString.index(inputString.startIndex, offsetBy: position) | |
| let endIndex = inputString.index(inputString.endIndex, offsetBy: -position - 1) | |
| if inputString[startIndex] == inputString[endIndex] { | |
| position += 1 | |
| } else { | |
| return false | |
| } | |
| } | |
| return true | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment