How to check if a string is title case in Swift

1 Answer

0 votes
import Foundation

func isTitleCase(_ s: String) -> Bool {
    for word in s.split(separator: " ") {
        guard let first = word.first, first.isUppercase else {
            return false
        }
        if word.dropFirst().contains(where: { !$0.isLowercase }) {
            return false
        }
    }
    return true
}

print(isTitleCase("Hello World"))   // true
print(isTitleCase("Hello world"))   // false




/*
run:

true
false

*/

 



answered 1 hour ago by avibootz
edited 1 hour ago by avibootz
...