Problem overview
Instead of asking whether a password has each kind of character, this version counts how many it has. You start with let password = "Hello!World9", and the program prints four lines: the number of uppercase letters, lowercase letters, digits and symbols. A symbol is any character that is not a letter and not a digit.
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
In "Hello!World9", the capitals are H and W, the digit is 9 and the symbol is the exclamation mark. The other eight characters are lowercase letters. 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 exclamation mark.
Approach: four counters, one loop
The earlier versions kept a yes-or-no flag per class. A count needs a number instead: four integer counters starting at 0, one for each class. The program walks through the characters once and, for each one, adds 1 to the counter of the class it belongs to.
Counting only changes the type of the variable and the update. A flag is set with = true, while a counter is raised with += 1. The class tests themselves are the ones used for flags. The four classes are tested in four separate if statements, so each test stands alone. For ordinary passwords, every character raises exactly one counter, and the four counts add up to the length.
A count carries more information than a flag: Digits: 0 says the same as Has digit: False, but Digits: 3 also says how many. Nothing is lost by moving from flags to counts, so this version is a strict upgrade of the previous idea. Only the printed lines change, and the verdict rules of the earlier versions are not part of this one.
Step by step
- Create
uppercaseas avarset to 0. - Create
lowercasethe same way. - Create
digits, the counter for the characters 0 to 9. - Create
symbols, the counter for everything that is neither a letter nor a digit. A space belongs here. - Start the loop:
for character in passwordruns once per character. For"Hello!World9"that is 12 times. - Count uppercase letters. If
character.isUppercaseis true,uppercase += 1. - Count lowercase letters with
character.isLowercase, in its ownif. - Count digits with
character.isNumber. - Count symbols when
!character.isLetter && !character.isNumber: not a letter and not a digit. - Print the four counters after the loop, one line each, in the order Uppercase, Lowercase, Digits, Symbols. Each line is a string literal with the counter interpolated, such as
"Uppercase: \(uppercase)".
The solution in Swift
let password = "Hello!World9"
var uppercase = 0
var lowercase = 0
var digits = 0
var symbols = 0
for character in password {
if character.isUppercase {
uppercase += 1
}
if character.isLowercase {
lowercase += 1
}
if character.isNumber {
digits += 1
}
if !character.isLetter && !character.isNumber {
symbols += 1
}
}
print("Uppercase: \(uppercase)")
print("Lowercase: \(lowercase)")
print("Digits: \(digits)")
print("Symbols: \(symbols)")
Swift details to note:
varand inferredInt.var uppercase = 0makes anIntwithout any annotation. It has to be avar, because+= 1assigns a new value.- There is no
++. Swift removed the increment operator, so a counter is raised with+= 1. - The properties follow Unicode.
isUppercaseandisLowercaseknow about accented letters. For"é1É_"the lesson's code printsUppercase: 1,Lowercase: 1,Digits: 1andSymbols: 1: the underscore is the symbol. - Counting characters. The loop sees what a reader would call a character, not bytes. A letter with an accent counts once.
- Overflow isn't a concern. The counters are 64-bit
Intvalues, and Swift stops the program on overflow instead of wrapping. A password can't be long enough to reach that.
Edge cases and common mistakes
- An empty password. The loop doesn't run, and all four lines print 0.
- Spaces. A space is not a letter and not a digit, so it counts as a symbol. In
"Pass word!2"that makes two symbols. - Expecting the counts to add up to the length. They do for ordinary passwords, but a letter from a script without cases, such as
"中", is a letter that is neither uppercase nor lowercase, and it isn't a symbol either. It raises no counter, so the four counts can add up to less than the length. - Counting inside the wrong place. The
printcalls belong after the loop. Inside it they would print four lines for every character. - Defining symbols by a list. A fixed list such as
"!@#"would miss characters like the space or the underscore. The "not a letter and not a digit" rule covers everything. - Using
letfor a counter. The compiler rejectsuppercase += 1on a constant. - Mixing up the order of the output. The lines come as Uppercase, Lowercase, Digits, Symbols.
Testing
Change password in the editor. These are results of the reference solution:
"Pass word!2"printsUppercase: 1,Lowercase: 7,Digits: 1andSymbols: 2.""prints zero on all four lines."é1É_"prints 1 on all four lines.
