Skip to content

Instantly share code, notes, and snippets.

@simonbromberg
Created August 11, 2016 02:48
Show Gist options
  • Save simonbromberg/de373d116ee1b8325ee4c435f44c8d8f to your computer and use it in GitHub Desktop.
Save simonbromberg/de373d116ee1b8325ee4c435f44c8d8f to your computer and use it in GitHub Desktop.
Swift 2 function to test if a string is a palindrome
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