Correct Capitalisation
1 min read

Correct Capitalisation

Given a string, return whether or not it uses capitalisation correctly. A string correctly uses capitalisation if all letters are capitalised, no letters are capitalised, or only the first letter is capitalised.

Ex: Given the following strings...

"USA", return true
"Calvin", return true
"compUter", return false
"coding", return true

Using Count

fun main() {
    val word = "LLR"
   
    print(detectCapital(word))
}

fun detectCapital(word: String): Boolean {
  var count = 0
    
   for(i in word.indices) {
    if(word[i].isUpperCase()) {
        count++
    }
   } 
   return count == word.length || count == 0 || count == 1 && word[0].isUpperCase()
}

Enjoying these posts? Subscribe for more