Created
January 27, 2020 21:35
-
-
Save waylonis/04fe29a078536bc45641cc6ac236bde1 to your computer and use it in GitHub Desktop.
Swift code to check if a string is a palindrome
This file contains 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(sequence: String) -> Bool { | |
// Start the indexes at the beginning and ending of the string | |
var findex = sequence.startIndex; | |
var bindex = sequence.index(before: sequence.endIndex); | |
// While they don't meet, check if the characters match | |
while findex < bindex { | |
if sequence[findex] != sequence[bindex] { | |
return false | |
} | |
// Since we're not comparing whitespace, we need to skip over any of them | |
repeat { | |
findex = sequence.index(after: findex) | |
} while sequence[findex].isWhitespace | |
repeat { | |
bindex = sequence.index(before: bindex) | |
} while sequence[bindex].isWhitespace | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment