Problem overview
The earlier versions asked yes-or-no questions about a password. This one asks how many. The program counts the uppercase letters, the lowercase letters, the digits and the symbols in val password = "Hello!World9" and prints four lines, one count each, in that order. A symbol is any character that is neither a letter nor a digit.
For Hello!World9 the output is:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
The two capitals are H and W, the digit is 9 and the symbol is !. For Pass word!2 the output is Uppercase: 1, Lowercase: 7, Digits: 1 and Symbols: 2, because the space counts as a symbol along with the !.
Approach: from flags to counters
A flag only remembers whether something happened. A counter remembers how many times. The change from the previous idea is small: instead of setting a variable to true, the loop adds 1 to an integer that started at 0. The structure is the same, one pass over the characters with one independent test per class.
Every character should land in exactly one counter. A letter is either uppercase or lowercase, a digit is a digit, and the rest are symbols, so the four counts add up to the length of the password. For Hello!World9 that is 2 + 8 + 1 + 1 = 12, which is the number of characters in the password. If the sum does not match the length, a test is wrong.
Step by step
- Create
uppercaseas avarset to 0. It is avarbecause its value changes during the loop. - Create
lowercasethe same way. - Create
digitsthe same way. - Create
symbolsthe same way. It counts every character that is not a letter and not a digit. - Loop over the characters with
for (c <- password). - Count capitals: if
c.isUpper, add 1 touppercasewith+= 1. - Count lowercase letters with
c.isLower. - Count digits with
c.isDigit. - Count symbols with
!c.isLetterOrDigit. - After the loop, print the four lines, each as a label followed by its counter. The
+converts theIntto text.
The solution in Scala
val password = "Hello!World9"
var uppercase = 0
var lowercase = 0
var digits = 0
var symbols = 0
for (c <- password) {
if (c.isUpper) {
uppercase += 1
}
if (c.isLower) {
lowercase += 1
}
if (c.isDigit) {
digits += 1
}
if (!c.isLetterOrDigit) {
symbols += 1
}
}
println("Uppercase: " + uppercase)
println("Lowercase: " + lowercase)
println("Digits: " + digits)
println("Symbols: " + symbols)
Notes on the Scala:
+= 1is shorthand foruppercase = uppercase + 1. It works on avarof a numeric type. There is no++operator in Scala.- Four separate
ifs. Each one tests a different class, and a character matches at most one of them, so the four counters never double-count a character. Intcounters. AnIntis 32 bits and wraps around silently when it overflows, but a counter limited by the password length never gets near that limit.- Printing numbers. A counter does not print as
TrueorFalse. It prints as a plain number, so"Digits: " + digitsneeds no conversion helper.
Following the loop
For Hello!World9 the characters arrive in order: H adds to uppercase; e, l, l and o add to lowercase; ! adds to symbols; W adds to uppercase; o, r, l and d add to lowercase again; and 9 adds to digits. The final counts are 2, 8, 1 and 1, matching the output above. Each character takes exactly one of the four paths, so the counters never compete for the same character.
Letters from other alphabets are classified too: isUpper and isLower are not limited to English letters, so an accented é is counted as lowercase. That is the behaviour you want from a general password counter.
A shorter form
Scala strings have a count method that takes a test and returns how many characters pass it, so "a b!".count(c => !c.isLetterOrDigit) gives 2. Four calls to count would replace the loop. The lesson uses the loop and the += 1 counters, which show the mechanism that count hides, and make one pass instead of four.
Complexity
One pass over n characters with four constant-time tests each gives O(n) time. The extra space is four integers, which is O(1).
Edge cases and common mistakes
- The space is a symbol. In
Pass word!2the space and the!both count, soSymbols: 2. - Empty password. The loop does not run, and all four lines show 0.
- Using
valfor a counter. The compiler rejectsuppercase += 1on aval, because avalcannot be reassigned. - Printing inside the loop. The print lines go after the closing brace. Inside the loop they would print four lines for every character.
- Counting letters as symbols. A symbol test of
!c.isLetteralone would count the digits twice, once as digits and once as symbols. The test has to exclude both letters and digits. - Swapping the order of lines. The output must read Uppercase, Lowercase, Digits and Symbols.
Testing
| Password | Uppercase | Lowercase | Digits | Symbols |
|---|---|---|---|---|
Hello!World9 | 2 | 8 | 1 | 1 |
Pass word!2 | 1 | 7 | 1 | 2 |
| (empty) | 0 | 0 | 0 | 0 |
In every row the four counts add up to the password's length, which is a quick way to check your own solution.
