@@ -2,7 +2,7 @@ import Cocoa
22
33public func palindromeCheck ( text: String ? ) -> Bool {
44 if let text = text {
5- let mutableText = text. stringByTrimmingCharactersInSet ( . whitespaceCharacterSet ( ) ) . lowercaseString
5+ let mutableText = text. trimmingCharacters ( in : NSCharacterSet . whitespaces ( ) ) . lowercased ( )
66 let length : Int = mutableText. characters. count
77
88 guard length >= 1 else {
@@ -11,26 +11,26 @@ public func palindromeCheck (text: String?) -> Bool {
1111
1212 if length == 1 {
1313 return true
14- } else if mutableText [ mutableText. startIndex] == mutableText [ mutableText. endIndex . predecessor ( ) ] {
15- let range = Range < String . Index > ( mutableText. startIndex . successor ( ) ..< mutableText. endIndex . predecessor ( ) )
16- return palindromeCheck ( mutableText. substringWithRange ( range) )
14+ } else if mutableText [ mutableText. startIndex] == mutableText [ mutableText. index ( mutableText . endIndex , offsetBy : - 1 ) ] {
15+ let range = Range < String . Index > ( mutableText. index ( mutableText . startIndex , offsetBy : 1 ) ..< mutableText. index ( mutableText . endIndex , offsetBy : - 1 ) )
16+ return palindromeCheck ( text : mutableText. substring ( with : range) )
1717 }
1818 }
1919
2020 return false
2121}
2222
2323// Test to check that non-palindromes are handled correctly:
24- palindromeCheck ( " owls " )
24+ palindromeCheck ( text : " owls " )
2525
2626// Test to check that palindromes are accurately found (regardless of case and whitespace:
27- palindromeCheck ( " lol " )
28- palindromeCheck ( " race car " )
29- palindromeCheck ( " Race fast Safe car " )
27+ palindromeCheck ( text : " lol " )
28+ palindromeCheck ( text : " race car " )
29+ palindromeCheck ( text : " Race fast Safe car " )
3030
3131// Test to check that palindromes are found regardless of case:
32- palindromeCheck ( " HelloLLEH " )
32+ palindromeCheck ( text : " HelloLLEH " )
3333
3434// Test that nil and empty Strings return false:
35- palindromeCheck ( " " )
36- palindromeCheck ( nil )
35+ palindromeCheck ( text : " " )
36+ palindromeCheck ( text : nil )
0 commit comments