Start learning free

LESSON 201 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in Swift: A Score from 0 to 100

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

let password = "Secret!99"

Build:

Length: 9
Score: 100

Rules:

  • Start the score at 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use the same character property checks, then accumulate into var score.

let password = "Secret!99"

let length = password.count
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
    }
}
var score = 0
if length >= 8 {
    score += 25
}
if hasUpper {
    score += 20
}
if hasLower {
    score += 20
}
if hasDigit {
    score += 20
}
if hasSymbol {
    score += 15
}
print("Length: \(length)")
print("Score: \(score)")

STEP 1 OF 18
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in Swift, explained

    Problem overview

    This version turns the earlier yes-or-no checks into one number. A password earns points for each good property, and the program prints its length and its score. You start with let password = "Secret!99". The points are:

    • 25 for a length of at least 8;
    • 20 for an uppercase letter;
    • 20 for a lowercase letter;
    • 20 for a digit;
    • 15 for a symbol, which is any character that is not a letter and not a digit.

    The five amounts add up to 100. For "Secret!99" the output is:

    Length: 9
    Score: 100

    For "abc123" the output is Length: 6 and Score: 40: it has lowercase letters and digits, but no capital, no symbol and not enough characters.

    Approach: find the facts, then add up the points

    The solution has two stages. The first collects facts about the password: its length, and four flags saying whether an uppercase letter, a lowercase letter, a digit and a symbol occur. A single loop over the characters sets the flags, as in the earlier versions of the checker. The second stage turns facts into points. It starts the score at 0 and runs five independent if statements, each adding its own amount when its fact is true.

    The score rules are separate ifs, not an if/else if chain, because the rules aren't alternatives. A password can earn points from every rule, so no rule may skip the others. Keeping the two stages apart also makes the scoring easy to change: another weight is a one-number edit in one place, and the loop stays as it is.

    The score is a measure, not a verdict: this version prints the number and leaves it to the reader to decide what a good score is. The input is a password that is already fixed in the code, so the program has no branch for a wrong kind of input. Whatever text it gets, the answer is a number from 0 to 100.

    Step by step

    1. Store the length. let length = password.count is used twice, for the score rule and for the printed line.
    2. Create hasUpper, hasLower, hasDigit and hasSymbol as var flags starting at false, one step each. A flag only ever changes from false to true.
    3. Loop over the characters. for character in password runs once per character, 9 times for "Secret!99".
    4. Raise the flags. In four separate ifs, isUppercase sets hasUpper, isLowercase sets hasLower, isNumber sets hasDigit, and !character.isLetter && !character.isNumber sets hasSymbol.
    5. Create score as a var set to 0, after the loop.
    6. Score the length. If length >= 8, score += 25.
    7. Score the four classes. One if per flag: 20 for hasUpper, 20 for hasLower, 20 for hasDigit and 15 for hasSymbol.
    8. Print the results. The Length: line first and the Score: line second, after the score is final.

    The solution in Swift

            let password = "Secret!99"
    
            let length = password.count
            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
                }
            }
            var score = 0
            if length >= 8 {
                score += 25
            }
            if hasUpper {
                score += 20
            }
            if hasLower {
                score += 20
            }
            if hasDigit {
                score += 20
            }
            if hasSymbol {
                score += 15
            }
            print("Length: \(length)")
            print("Score: \(score)")
    

    The Swift details worth knowing:

    • A Bool is a complete condition. if hasUpper { needs no comparison with true. The flags are declared as var because the loop assigns them, and score because it is raised with +=.
    • Inferred types. var score = 0 is an Int and var hasUpper = false is a Bool. No annotations are needed.
    • The properties follow Unicode. isUppercase, isLowercase, isLetter and isNumber are Bool properties of Character, and they cover accented letters as well as the English alphabet.
    • Length counts characters. count gives the number of characters, so an accented letter counts as one.
    • The maximum is 100 by construction. The five amounts sum to 100, so no check against going over is needed.

    Edge cases and common mistakes

    • Long but one-dimensional. Length alone is worth only 25 points. A password of 8 or more characters with a single class of character can't reach even half of the 100.
    • The empty password. No flag is raised and the length is 0, so the output is Length: 0 and Score: 0.
    • Only one class, long enough. "ABCDEFGH" scores 45: 25 for the length and 20 for the uppercase letters.
    • Adding a symbol. "abcdefgh!" scores 60: 25 for the length, 20 for lowercase and 15 for the symbol.
    • Using else if for the rules. After the first matching rule the others would be skipped, and the score could never pass 25.
    • Counting a class once per character. Adding 20 inside the loop for every uppercase letter would give each capital its own 20 points. The flags make each class worth its points once, however many characters belong to it.
    • Printing before the score is complete. The two print lines come after all the ifs. Earlier, they would show a partial score.

    Testing

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

    • "abc123" prints Length: 6 and Score: 40.
    • "" prints Length: 0 and Score: 0.
    • "ABCDEFGH" prints Length: 8 and Score: 45.
    • "abcdefgh!" prints Length: 9 and Score: 60.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free