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
- Store
lengthas avalfrompassword.length. It is used both for printing and for the length rule. - Print the first line,
"Length: "followed by the number. - Create
hasDigit. It is avar, because it will change, and it starts atfalse. - 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. - Test each character. Inside the loop, an
ifchecksc.isDigit()and setshasDigittotrue. - Print the flag after the loop, as
TrueorFalse. This line runs once, when every character has been checked. - Write the verdict. An
ifonlength >= 8 && hasDigitprintsStrong. - Add the
else, which printsWeak. 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 aString, andchas typeChar. There is no need to index by position. ifis 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 wholeif/elsetogether as the right-hand operand of+. Kotlin has no?:ternary operator for this; theifexpression takes its place, andelseis required when it is used as a value.Booleanprints in lowercase.println(true)showstrue, 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 passwordabc٣givesHas 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 digitline would repeat once per character instead of once. valinstead ofvar. Avalflag 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.
