Start learning free

LESSON 198 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in Swift: Four Character Class Flags

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

let password = "Secret!99"

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Start four flags (hasUpper, hasLower, hasDigit, hasSymbol) at false. Loop over every character and set the matching flag to true.
  • A symbol is any character that is not a letter and not a digit.
  • Print one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and all four flags are true (stricter than the last lesson), print Strong; otherwise print Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

Use c.isUppercase, c.isLowercase, c.isNumber, !c.isLetter && !c.isNumber in a for c in password loop.

let password = "Secret!99"

let length = password.count
print("Length: \(length)")
var hasUpper = false
var hasLower = false
var hasDigit = false
var hasSymbol = false
for character in password {
    if character.isUppercase {
        hasUpper = true
    }
    if character.isLowercase {
        hasLower = true
    }
    if character.isNumber {
        hasDigit = true
    }
    if !character.isLetter && !character.isNumber {
        hasSymbol = true
    }
}
print("Upper: \(hasUpper ? "True" : "False")")
print("Lower: \(hasLower ? "True" : "False")")
print("Digit: \(hasDigit ? "True" : "False")")
print("Symbol: \(hasSymbol ? "True" : "False")")
if length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol {
    print("Strong")
} else {
    print("Weak")
}

STEP 1 OF 13
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in Swift, explained

    Problem overview

    The checker now wants variety. A password is strong when it has at least 8 characters and contains an uppercase letter, a lowercase letter, a digit and a symbol. A symbol is any character that is not a letter and not a digit. You start with let password = "Secret!99", and the program prints the length, one line per class and a verdict:

    Length: 9
    Upper: True
    Lower: True
    Digit: True
    Symbol: True
    Strong

    For "secret99" the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak. The booleans print as True and False, with a capital letter.

    Approach: four flags, one pass

    The previous version used one flag for digits. This one scales the idea up: hasUpper, hasLower, hasDigit and hasSymbol, all starting at false. A single loop over the characters can raise all four. For every character, the program asks four questions, one if for each.

    The four tests are separate if statements, not an if/else if chain. A chain stops at the first test that matches, and the others are never asked. Separate ifs each test the character on their own, so adding a fifth class later means adding one more block and touching nothing else.

    The classes aren't a partition of all characters. A letter from a script without cases, such as "中", is a letter, but it is neither uppercase nor lowercase: it raises none of the first two flags and is not a symbol either. Four independent tests handle that case without any special code.

    Flags are enough here because the rule asks only whether each class appears, not how often. Once a flag is true it stays true, and the loop could not do better by remembering more. The length is a separate rule: it comes from count and doesn't need the loop at all.

    Step by step

    1. Store the length in length and print Length: with it.
    2. Create the first two flags, hasUpper and hasLower, as var values starting at false.
    3. Create the other two, hasDigit and hasSymbol, in the same way.
    4. Start the loop: for character in password hands you one character at a time. One pass checks all four classes.
    5. Uppercase test. character.isUppercase sets hasUpper.
    6. Lowercase test. character.isLowercase sets hasLower, in its own if after the first.
    7. Digit test. character.isNumber sets hasDigit.
    8. Symbol test. A symbol is neither a letter nor a digit, so the condition is !character.isLetter && !character.isNumber. The ! in front of each part flips true and false.
    9. Print the first two flags after the loop, Upper and Lower, using flag ? "True" : "False" inside the interpolation.
    10. Print the other two, Digit and Symbol, keeping the order: Upper, Lower, Digit, Symbol.
    11. Write the verdict. Strong needs five conditions joined with &&: the length and the four flags.
    12. Add the else: a password that misses any one of the five gets Weak.

    The solution in Swift

            let password = "Secret!99"
    
            let length = password.count
            print("Length: \(length)")
            var hasUpper = false
            var hasLower = false
            var hasDigit = false
            var hasSymbol = false
            for character in password {
                if character.isUppercase {
                    hasUpper = true
                }
                if character.isLowercase {
                    hasLower = true
                }
                if character.isNumber {
                    hasDigit = true
                }
                if !character.isLetter && !character.isNumber {
                    hasSymbol = true
                }
            }
            print("Upper: \(hasUpper ? "True" : "False")")
            print("Lower: \(hasLower ? "True" : "False")")
            print("Digit: \(hasDigit ? "True" : "False")")
            print("Symbol: \(hasSymbol ? "True" : "False")")
            if length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol {
                print("Strong")
            } else {
                print("Weak")
            }
    

    Swift details in this solution:

    • The properties live on Character. isUppercase, isLowercase, isLetter and isNumber are all Bool properties, with no parentheses and no imports. They follow Unicode, so "É" counts as uppercase and "é" as lowercase, not only the English alphabet.
    • Symbols are defined by exclusion. There is no isSymbol in the lesson's code: a symbol is what is left after letters and digits. A space therefore counts as a symbol. With "Secret 99" the lesson's code prints Symbol: True and Strong.
    • The ternary is repeated four times. Swift prints a Bool as true or false, so each line picks its own text to get the capital letter.
    • && chains. Five conditions joined with && are all required, and the evaluation stops at the first false one.
    • An underscore is a symbol. It is neither a letter nor a number in Unicode terms, so _ sets hasSymbol.

    Edge cases and common mistakes

    • Missing a class. "SECRET!99" has no lowercase letter: it prints Lower: False and Weak. "Secret!!!" has no digit and is Weak too.
    • Forgetting one of the five conditions. Leaving out length >= 8 would call a short password such as "Ab1!" strong.
    • Using else if between the tests. The chain would skip every test after the first match, which couples the tests to each other. Separate ifs remove that risk.
    • Writing the symbol test as ||. !isLetter || !isNumber is true for ordinary characters such as a (not a number) and 9 (not a letter), so it would flag almost every character as a symbol. The two negations must be joined with &&.
    • Printing the flags inside the loop. Each flag line should appear once, after the loop has seen the whole password.
    • Changing the order of the lines. The output is Upper, Lower, Digit, Symbol, in that order.

    Testing

    Change password in the editor. These results come from the reference solution:

    • "secret99" prints Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • "SECRET!99" prints Length: 9, Upper: True, Lower: False, Digit: True, Symbol: True and Weak.
    • "Secret!!!" prints Length: 9, Upper: True, Lower: True, Digit: False, Symbol: True and Weak.
    • "Secret 99" prints Length: 9, all four flags as True and Strong.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free