Problem overview
This is the first version of a small password strength checker. It looks at one thing only, the length. You start with a constant password holding "Secret99". The program prints the number of characters, then a verdict: OK for 8 characters or more, and Too short for fewer.
Password length: 8
OK
For "abc" the output is Password length: 3 followed by Too short. Later versions of the checker build on this one.
Approach: measure once, use twice
The length is needed in two places: it is printed, and it is compared with 8. So the program computes it once, stores it in a constant and uses the constant in both places. After that, one if/else chooses which of the two verdicts to print.
The boundary matters: a password of exactly 8 characters is acceptable, so the condition is "greater than or equal to 8", written length >= 8.
Step by step
- Store the length.
let length = password.count. In Swift a string's length is itscountproperty. There are no parentheses, because it is a property and not a method. - Print the first line.
print("Password length: \(length)")puts the number inside the text with string interpolation, so no conversion is needed. - Write the
if. Whenlength >= 8, printOK. - Add the
else. It runs for every length theifdidn't accept, which is 7 or fewer, and printsToo short. In Swift style theelsefollows the closing brace on the same line.
The solution in Swift
let password = "Secret99"
let length = password.count
print("Password length: \(length)")
if length >= 8 {
print("OK")
} else {
print("Too short")
}
The Swift details here:
countcounts characters, not bytes. Swift treats what a reader sees as one character as a singleCharacter, even when it takes several bytes."héllo wörld"has a length of 11, and eight thumbs-up emoji have a length of 8. A language that reports the byte size would give larger numbers for the same text.- Why not
password[0]? AStringcan't be indexed with an integer in Swift, because finding the n-th character means walking through the text. Counting needs no indexing, socountis all this version requires. - The condition has no parentheses.
if length >= 8 {is the usual Swift form. The braces are required, even for a single statement. - The condition must be a
Bool. Swift doesn't treat a number as true or false, soif length {is a compile error. Compare the number with something. letfor a value that never changes.lengthis computed once and never reassigned, so it is a constant.
Edge cases and common mistakes
- Exactly 8 characters.
"Secret99"has a length of 8 and printsOK. Writinglength > 8would reject it, which is an off-by-one error. - Seven characters.
"abcdefg"printsPassword length: 7andToo short. - The empty string. It has a length of 0, so the output is
Password length: 0andToo short. - Spaces count. A space is a character, so
"abc def"has a length of 7. The program doesn't trim the text. - Writing
password.count(). With parentheses it doesn't compile, becausecountis a property. - Printing the number with
+."Password length: " + lengthdoesn't compile, since aStringand anIntcan't be added. Interpolation avoids the problem.
Testing
Change password in the editor. The outputs below come from the reference solution:
"abc"printsPassword length: 3andToo short."abcdefg"printsPassword length: 7andToo short.""printsPassword length: 0andToo short."héllo wörld"printsPassword length: 11andOK.
