Start learning free

LESSON 200 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in Kotlin: Scoring from 0 to 100

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

val password = "Secret!99"

Build:

Length: 9
Score: 100

Rules:

  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use the same c.isUpperCase(), c.isLowerCase(), c.isDigit(), c.isLetterOrDigit() checks, then accumulate into var score.

val password = "Secret!99"

val length = password.length
var hasUpper = false
var hasLower = false
var hasDigit = false
var hasSymbol = false
for (c in password) {
    if (c.isUpperCase()) {
        hasUpper = true
    }
    if (c.isLowerCase()) {
        hasLower = true
    }
    if (c.isDigit()) {
        hasDigit = true
    }
    if (!c.isLetterOrDigit()) {
        hasSymbol = true
    }
}
var score = 0
if (length >= 8) {
    score += 25
}
if (hasUpper) {
    score += 20
}
if (hasLower) {
    score += 20
}
if (hasDigit) {
    score += 20
}
if (hasSymbol) {
    score += 15
}
println("Length: $length")
println("Score: $score")

STEP 1 OF 18
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in Kotlin, explained

    Problem overview

    This version turns the earlier checks into one number. The program starts with val password = "Secret!99" and awards points for the length and for each class of character:

    • 25 points if the length is at least 8;
    • 20 points for an uppercase letter;
    • 20 points for a lowercase letter;
    • 20 points for a digit;
    • 15 points for a symbol, meaning a character that is neither a letter nor a digit.

    The five amounts add up to exactly 100. The program prints the length and the score:

    Length: 9
    Score: 100

    For abc123, which is 6 characters long and has lowercase letters and digits only, the score is 40:

    Length: 6
    Score: 40

    Approach: find the facts, then add the points

    The work splits into two phases. In the first, one loop over the characters sets four boolean flags, as in the earlier version with character classes. In the second, a series of independent if statements adds points to a score variable, one rule per statement.

    Each rule must be its own if, not part of an else if chain. A chain stops at the first match, so a password with an uppercase letter would never be checked for the other classes. The rules are not alternatives; they are additive, and every rule that applies contributes its points.

    Keeping the two phases apart makes the scoring easy to read and to change. To adjust what a digit is worth, you change one number in one line, without touching the loop.

    Here is how the second example gets its 40. abc123 has 6 characters, so the length rule adds nothing. It has no uppercase letter and no symbol, so those rules add nothing either. It has lowercase letters, worth 20, and digits, worth 20. The total is 40. The program's output for it, shown above, is the same.

    Step by step

    1. Store length from password.length. It is used for the length rule and for the first output line.
    2. Create the four flags hasUpper, hasLower, hasDigit and hasSymbol, each a var starting at false.
    3. Loop over the characters with for (c in password).
    4. Set the flags. Four if statements test c.isUpperCase(), c.isLowerCase(), c.isDigit() and !c.isLetterOrDigit(). Each sets its own flag to true, and none ever sets one back.
    5. Create score as a var at 0.
    6. Add the length points. If length >= 8, score += 25.
    7. Add the class points. One if each for hasUpper, hasLower and hasDigit adds 20, and one for hasSymbol adds 15.
    8. Print the two lines, the length first, then the score.

    The solution in Kotlin

            val password = "Secret!99"
    
            val length = password.length
            var hasUpper = false
            var hasLower = false
            var hasDigit = false
            var hasSymbol = false
            for (c in password) {
                if (c.isUpperCase()) {
                    hasUpper = true
                }
                if (c.isLowerCase()) {
                    hasLower = true
                }
                if (c.isDigit()) {
                    hasDigit = true
                }
                if (!c.isLetterOrDigit()) {
                    hasSymbol = true
                }
            }
            var score = 0
            if (length >= 8) {
                score += 25
            }
            if (hasUpper) {
                score += 20
            }
            if (hasLower) {
                score += 20
            }
            if (hasDigit) {
                score += 20
            }
            if (hasSymbol) {
                score += 15
            }
            println("Length: $length")
            println("Score: $score")
    

    Kotlin details worth a closer look:

    • +=. score += 25 means score = score + 25. It needs a var.
    • Testing a boolean directly. if (hasUpper) is enough; writing if (hasUpper == true) says the same thing in more words.
    • Templates. "Length: $length" inserts the value of the variable. A template reads up to the end of the name, so "Score: $score" prints Score: 100 and no stray characters come along.
    • Flags and counters are different tools. The flags only record that a class is present. Scoring needs just that, not how many characters there were, so a password with ten digits earns the same 20 points as one with a single digit.
    • Int arithmetic. The largest possible score is 100, so there is no overflow concern, and the sum is exact.

    A score is a different kind of answer from the earlier verdicts. A verdict is yes or no, while a score can say how far a password is from the top. That also means the program must define what the numbers mean: here the maximum, 100, is reached only when every rule passes.

    Complexity

    The loop visits each character once, and the scoring does a fixed amount of work. Time is proportional to the password length, and the space is a few variables.

    Edge cases and common mistakes

    • else if instead of separate ifs. The score would stop growing after the first rule that matched.
    • Counting a class twice. Adding points inside the character loop would award 20 points for every uppercase letter, not once. The flags exist to avoid that.
    • Printing the score before it is final. The output comes after all the rules have run.
    • Short but varied passwords. A password can lose the 25 length points and still score from its classes.

    Testing

    The empty password has no length points and no classes, so it scores 0:

    Length: 0
    Score: 0

    The 4-character password Aa1! has all four classes but fails the length rule, so it gets 20 + 20 + 20 + 15:

    Length: 4
    Score: 75

    These two cases show the extremes of the scale and the effect of the length rule on its own.

    Keep going with Kotlin1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free