Created
March 11, 2017 08:57
-
-
Save SirWellington/9891ffa86b21cd4b8f4dc91daa1ce700 to your computer and use it in GitHub Desktop.
Swift - Palindrome Test
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
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