import Foundation
func verifyCasing(_ word: String) -> Bool {
let upper = word.filter { $0.isUppercase }.count
let lower = word.filter { $0.isLowercase }.count
// Case 1: all lowercase
if upper == 0 { return true }
// Case 2: all uppercase
if lower == 0 { return true }
// Case 3: capitalized (only first letter uppercase)
if upper == 1, let first = word.first, first.isUppercase {
return true
}
// Otherwise, mixed casing
return false
}
func runTest(_ word: String) {
print("Testing word: \"\(word)\"")
if verifyCasing(word) {
print("OK\n")
} else {
print("Error\n")
}
}
runTest("PROGRAMMING")
runTest("programming")
runTest("Programming")
runTest("ProGramMing")
/*
run:
Testing word: "PROGRAMMING"
OK
Testing word: "programming"
OK
Testing word: "Programming"
OK
Testing word: "ProGramMing"
Error
*/