Skip to content

Instantly share code, notes, and snippets.

@SirWellington
Created March 11, 2017 08:57
Show Gist options
  • Save SirWellington/9891ffa86b21cd4b8f4dc91daa1ce700 to your computer and use it in GitHub Desktop.
Save SirWellington/9891ffa86b21cd4b8f4dc91daa1ce700 to your computer and use it in GitHub Desktop.
Swift - Palindrome Test
import Foundation
for _ in 1...5 {
print("Hello, World!")
}
func reverseString(_ string: String) -> String {
var result = ""
for character in string.characters.reversed() {
result += "\(character)"
}
return result
}
var test = reverseString("CoderPad")
print(test)
test = reverseString("Mariana")
print(test)
func isPalindrone(string: String, ignoreCase: Bool = false) -> Bool {
if ignoreCase {
let reversed = reverseString(string).lowercased()
let original = string.lowercased()
print("\(original) == \(reversed)")
return original == reversed
}
else {
let reversed = reverseString(string)
return string == reversed
}
}
var result = isPalindrone(string: "mom")
print(result)
result = isPalindrone(string: "Mom")
print(result)
result = isPalindrone(string: "Mom", ignoreCase: true)
print(result)
result = isPalindrone(string: "some random string")
print(result)
result = isPalindrone(string: "Dad Mom dad", ignoreCase: true)
print(result)
result = isPalindrone(string: "Mom Dad Mom", ignoreCase: false)
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment