Skip to content

Instantly share code, notes, and snippets.

@tmm
Last active August 6, 2020 15:42
Show Gist options
  • Save tmm/a65837676afce88138985c09a04e7a4a to your computer and use it in GitHub Desktop.
Save tmm/a65837676afce88138985c09a04e7a4a to your computer and use it in GitHub Desktop.
import UIKit
//"USA", return true
//"Calvin", return true
//"compUter", return false
//"coding", return true
func isUppercase(character: Character) -> Bool {
String(character) == character.uppercased()
}
func isCorrectCapitalization(_ string: String) -> Bool {
var prevChar: Character?
for char in string {
if prevChar != nil &&
isUppercase(character: char) &&
!isUppercase(character: prevChar!) {
return false
}
prevChar = char
}
return true
}
isCorrectCapitalization("USA") == true
isCorrectCapitalization("Calvin") == true
isCorrectCapitalization("compUter") == false
isCorrectCapitalization("coding") == true
isCorrectCapitalization("HELLO") == true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment