Skip to content

Instantly share code, notes, and snippets.

@waylonis
Created January 27, 2020 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save waylonis/04fe29a078536bc45641cc6ac236bde1 to your computer and use it in GitHub Desktop.
Save waylonis/04fe29a078536bc45641cc6ac236bde1 to your computer and use it in GitHub Desktop.
Swift code to check if a string is a palindrome
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