Start learning free

LESSON 196 OF 291 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V2 in Kotlin: Has a Digit Flag

Practice Read the full explanation

Extend the length check with a second rule: the password must also contain at least one digit.

You start with:

val password = "Secret99"

Build:

Length: 8
Has digit: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Loop through each character to set a boolean flag hasDigit, which starts as false and becomes true when a digit is seen.
  • Print "Has digit: " followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and hasDigit is true, print Strong; otherwise print Weak.

Try other passwords: with abcdefgh the output is

Length: 8
Has digit: False
Weak

c.isDigit() tests a single char. Use (if (hasDigit) "True" else "False") to print a capitalised True/False.

val password = "Secret99"

val length = password.length
println("Length: " + length)
var hasDigit = false
for (c in password) {
    if (c.isDigit()) {
        hasDigit = true
    }
}
println("Has digit: " + (if (hasDigit) "True" else "False"))
if (length >= 8 && hasDigit) {
    println("Strong")
} else {
    println("Weak")
}

STEP 1 OF 8
    Ctrl + Enter to check

    KOTLIN · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V2 in Kotlin, explained

    Problem overview

    This version adds a second rule to the length check: the password must also contain at least one digit. The program starts with val password = "Secret99". It prints the length, reports whether a digit was found, and gives a verdict of Strong or Weak.

    For Secret99:

    Length: 8
    Has digit: True
    Strong

    For abcdefgh, which is long enough but has no digit:

    Length: 8
    Has digit: False
    Weak

    The digit line must read True or False with a capital first letter, which is not how Kotlin prints a Boolean. The program has to choose the text itself.

    Approach: a flag set by a loop

    The length is one number. "Has a digit" is a question about every character, so the program has to look at all of them. The standard technique is a boolean flag: start it at false ("no digit seen yet"), walk through the characters, and flip it to true the moment a digit appears. After the loop, the flag answers the question.

    The flag only ever moves in one direction. Once a digit has been seen, nothing sets the flag back to false, so seeing more digits afterwards changes nothing. Setting it in an else branch would be a bug, because a later non-digit would undo it.

    The verdict then combines two rules with &&: the length is at least 8 and the flag is true. Both must hold.

    A check could also stop at the first digit, since nothing more can change the answer. The lesson's loop keeps going to the end, which is simpler to read and costs nothing noticeable for a password. The same flag-and-loop pattern works for any "does the text contain X" question, and the next versions use it for more kinds of character.

    Step by step

    1. Store length as a val from password.length. It is used both for printing and for the length rule.
    2. Print the first line, "Length: " followed by the number.
    3. Create hasDigit. It is a var, because it will change, and it starts at false.
    4. Loop over the characters. for (c in password) gives one character of the string per round. The closing brace goes in before the body is filled in.
    5. Test each character. Inside the loop, an if checks c.isDigit() and sets hasDigit to true.
    6. Print the flag after the loop, as True or False. This line runs once, when every character has been checked.
    7. Write the verdict. An if on length >= 8 && hasDigit prints Strong.
    8. Add the else, which prints Weak. It catches a short password, a password without a digit, and one that fails both.

    The solution in Kotlin

            val password = "Secret99"
    
            val length = password.length
            println("Length: " + length)
            var hasDigit = false
            for (c in password) {
                if (c.isDigit()) {
                    hasDigit = true
                }
            }
            println("Has digit: " + (if (hasDigit) "True" else "False"))
            if (length >= 8 && hasDigit) {
                println("Strong")
            } else {
                println("Weak")
            }
    

    Kotlin details worth a closer look:

    • A string is a sequence of characters. for (c in password) works directly on a String, and c has type Char. There is no need to index by position.
    • if is an expression. if (hasDigit) "True" else "False" produces a value, so it can be used inside a larger expression. The parentheses around it keep the whole if/else together as the right-hand operand of +. Kotlin has no ?: ternary operator for this; the if expression takes its place, and else is required when it is used as a value.
    • Boolean prints in lowercase. println(true) shows true, which is why the output text is chosen by hand.
    • Char.isDigit() is broader than 0 to 9. It is true for any character that Unicode classes as a decimal digit, which includes digits from other scripts. For example, the Arabic-Indic digit ٣ counts: the password abc٣ gives Has digit: True. For ordinary ASCII passwords this makes no difference.
    • && short-circuits. The right side is only evaluated if the left side is true. Here both sides are cheap, so it is only a matter of reading order.

    Complexity

    The loop visits each character once, so the time is proportional to the password length, and the extra space is a few variables.

    Edge cases and common mistakes

    • Resetting the flag. An else { hasDigit = false } inside the loop would make the flag depend only on the last character.
    • Printing inside the loop. The Has digit line would repeat once per character instead of once.
    • val instead of var. A val flag can't be reassigned, and the compiler reports an error when the loop tries.
    • Using || for the verdict. That would accept a password that satisfies either rule. The rule asks for both.

    Testing

    A short password with a digit, Ab1, passes the digit test but fails the length rule:

    Length: 3
    Has digit: True
    Weak

    This shows why the verdict combines the rules instead of reporting the digit alone. The two examples at the top cover the other combinations: long with a digit is Strong, long without one is Weak.

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