@@ -2,7 +2,7 @@ import Cocoa
2
2
3
3
public func palindromeCheck ( text: String ? ) -> Bool {
4
4
if let text = text {
5
- let mutableText = text. stringByTrimmingCharactersInSet ( . whitespaceCharacterSet ( ) ) . lowercaseString
5
+ let mutableText = text. trimmingCharacters ( in : NSCharacterSet . whitespaces ( ) ) . lowercased ( )
6
6
let length : Int = mutableText. characters. count
7
7
8
8
guard length >= 1 else {
@@ -11,26 +11,26 @@ public func palindromeCheck (text: String?) -> Bool {
11
11
12
12
if length == 1 {
13
13
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) )
17
17
}
18
18
}
19
19
20
20
return false
21
21
}
22
22
23
23
// Test to check that non-palindromes are handled correctly:
24
- palindromeCheck ( " owls " )
24
+ palindromeCheck ( text : " owls " )
25
25
26
26
// 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 " )
30
30
31
31
// Test to check that palindromes are found regardless of case:
32
- palindromeCheck ( " HelloLLEH " )
32
+ palindromeCheck ( text : " HelloLLEH " )
33
33
34
34
// Test that nil and empty Strings return false:
35
- palindromeCheck ( " " )
36
- palindromeCheck ( nil )
35
+ palindromeCheck ( text : " " )
36
+ palindromeCheck ( text : nil )
0 commit comments