Problem overview
Instead of asking whether a class of character is present, this version counts how many characters of each class the password has. For Hello!World9 the program prints four lines:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
The password has 12 characters: two capitals (H and W), eight lowercase letters, one digit and one symbol, the exclamation mark. Every character is counted exactly once. A symbol is any character that is not a letter and not a digit, so a space counts as one. For Pass word!2 the output is:
Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2
The two symbols there are the space and the exclamation mark. There is no verdict in this version: it only reports counts.
Approach: four counters
The previous version used four boolean flags, each flipped once. A flag only remembers "at least one". A counter remembers "how many", so the change from a flag to an integer is the whole idea here. Each counter starts at 0, and each matching character adds 1 to the counter of its class.
The loop has the same shape as before: one pass over the characters, four independent if tests inside it. The only difference is the action when a test succeeds: uppercase++ instead of hasUpper = true.
The counts carry more information than the flags did. Two lowercase letters and twenty give the same flag but very different counts, so a report built on counts can tell a password that barely meets a rule from one that exceeds it. This lesson only prints the numbers, but that is the reason to count.
The order of the work matters too. All the counting happens in the loop, and all the printing happens after it, because a count is only final once every character has been seen.
Step by step
- Create
uppercase. Avarstarting at 0. - Create
lowercase, also avarat 0. - Create
digits, for the characters 0 to 9. - Create
symbols, for everything that is neither a letter nor a digit. - Loop over the characters with
for (c in password). The closing brace goes in first. - Count capitals. If
c.isUpperCase(), douppercase++. - Count lowercase letters. If
c.isLowerCase(), dolowercase++. - Count digits. If
c.isDigit(), dodigits++. - Count symbols. If
!c.isLetterOrDigit(), dosymbols++. - Print the four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined with
+to its counter.
The solution in Kotlin
val password = "Hello!World9"
var uppercase = 0
var lowercase = 0
var digits = 0
var symbols = 0
for (c in password) {
if (c.isUpperCase()) {
uppercase++
}
if (c.isLowerCase()) {
lowercase++
}
if (c.isDigit()) {
digits++
}
if (!c.isLetterOrDigit()) {
symbols++
}
}
println("Uppercase: " + uppercase)
println("Lowercase: " + lowercase)
println("Digits: " + digits)
println("Symbols: " + symbols)
Kotlin details worth a closer look:
- Increment.
uppercase++adds 1 to the variable. It is shorthand foruppercase = uppercase + 1, and it requires avar. The counters areInts, which hold values up to about 2.1 billion, far beyond any password. - Counters must be
var. Avalcan't be incremented; the compiler rejects it. - Four tests, four ifs. The tests don't overlap here, but keeping them as separate
ifstatements means each counter depends only on its own rule. - Unicode-aware tests. The
Charmethods follow Unicode, so accented letters count as letters, and for letters that have a case, as uppercase or lowercase. - Printing numbers.
"Uppercase: " + uppercaseworks because the left operand is aString; Kotlin converts theIntto text.
Reading the loop
On each round, c holds one character, and all four tests run on it. For the exclamation mark in Hello!World9, the first three tests fail, because it is not a letter or a digit, and the fourth succeeds, so only symbols goes up. For a lowercase e, only the second test succeeds. Each character therefore changes exactly one counter, and the loop leaves each counter holding the total for its class.
Complexity
One pass over the password with a fixed number of tests per character: time proportional to the length, and constant extra space.
Edge cases and common mistakes
- Counters declared inside the loop. They would reset to 0 on every character. They must be created before the loop.
- Printing inside the loop. The four lines are printed once, after every character has been counted.
- Forgetting that a space is a symbol. In
Pass word!2the space is the reason the symbol count is 2, not 1. - Counts that don't add up. For these four classes, every character lands in exactly one counter, so the four numbers add up to the password's length. For
Hello!World9, 2 + 8 + 1 + 1 is 12. If a total comes out different, a test is wrong.
Testing
An empty password, "", never enters the loop body, and the program prints:
Uppercase: 0
Lowercase: 0
Digits: 0
Symbols: 0
The two examples at the top cover a mixed password with one symbol and one with a space.
