Start learning free

LESSON 196 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V1 in Swift: Length Check with count and if/else

Practice Read the full explanation

A tiny password strength checker that looks only at the length of the password.

You start with:

let password = "Secret99"

Build:

Password length: 8
OK

Rules:

  • Print "Password length: " followed by the number of characters.
  • If the length is at least 8, print OK; otherwise print Too short.

Try other passwords: with abc the output is

Password length: 3
Too short

Swift's String.count property gives you the number of characters.

let password = "Secret99"

let length = password.count
print("Password length: \(length)")
if length >= 8 {
    print("OK")
} else {
    print("Too short")
}

STEP 1 OF 4
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V1 in Swift, explained

    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

    1. Store the length. let length = password.count. In Swift a string's length is its count property. There are no parentheses, because it is a property and not a method.
    2. Print the first line. print("Password length: \(length)") puts the number inside the text with string interpolation, so no conversion is needed.
    3. Write the if. When length >= 8, print OK.
    4. Add the else. It runs for every length the if didn't accept, which is 7 or fewer, and prints Too short. In Swift style the else follows 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:

    • count counts characters, not bytes. Swift treats what a reader sees as one character as a single Character, 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]? A String can't be indexed with an integer in Swift, because finding the n-th character means walking through the text. Counting needs no indexing, so count is 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, so if length { is a compile error. Compare the number with something.
    • let for a value that never changes. length is 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 prints OK. Writing length > 8 would reject it, which is an off-by-one error.
    • Seven characters. "abcdefg" prints Password length: 7 and Too short.
    • The empty string. It has a length of 0, so the output is Password length: 0 and Too 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, because count is a property.
    • Printing the number with +. "Password length: " + length doesn't compile, since a String and an Int can't be added. Interpolation avoids the problem.

    Testing

    Change password in the editor. The outputs below come from the reference solution:

    • "abc" prints Password length: 3 and Too short.
    • "abcdefg" prints Password length: 7 and Too short.
    • "" prints Password length: 0 and Too short.
    • "héllo wörld" prints Password length: 11 and OK.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free