Created
August 11, 2016 02:48
-
-
Save simonbromberg/de373d116ee1b8325ee4c435f44c8d8f to your computer and use it in GitHub Desktop.
Swift 2 function to test if a string is a palindrome
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(string:String) -> Bool { | |
if string.isEmpty || string.characters.count == 1 { | |
return true | |
} | |
var fromStart = string.startIndex | |
var fromEnd = string.endIndex.predecessor() | |
while fromStart != fromEnd { | |
if string[fromStart] != string[fromEnd] { | |
return false | |
} | |
if fromStart.successor() == fromEnd { // if even length string | |
return string[fromStart] == string[fromEnd] | |
} | |
fromStart = fromStart.successor() | |
fromEnd = fromEnd.predecessor() | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment