Start learning free

LESSON 199 OF 292 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in Swift: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

let password = "Hello!World9"

Build:

Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1

Rules:

  • Use four integer counters, all starting at 0.
  • Loop through each character and add 1 to the matching counter.
  • Treat any character that is not a letter and not a digit as a symbol.
  • Print one line per counter, in this order: "Uppercase: ", "Lowercase: ", "Digits: ", "Symbols: ", each followed by the count.

Try other passwords: with Pass word!2 the output is

Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2

c.isUppercase, c.isLowercase, c.isNumber, !c.isLetter && !c.isNumber, with += 1 counters.

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)")

STEP 1 OF 13
    Ctrl + Enter to check

    SWIFT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in Swift, explained

    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

    1. Create uppercase as a var set to 0.
    2. Create lowercase the same way.
    3. Create digits, the counter for the characters 0 to 9.
    4. Create symbols, the counter for everything that is neither a letter nor a digit. A space belongs here.
    5. Start the loop: for character in password runs once per character. For "Hello!World9" that is 12 times.
    6. Count uppercase letters. If character.isUppercase is true, uppercase += 1.
    7. Count lowercase letters with character.isLowercase, in its own if.
    8. Count digits with character.isNumber.
    9. Count symbols when !character.isLetter && !character.isNumber: not a letter and not a digit.
    10. 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:

    • var and inferred Int. var uppercase = 0 makes an Int without any annotation. It has to be a var, because += 1 assigns a new value.
    • There is no ++. Swift removed the increment operator, so a counter is raised with += 1.
    • The properties follow Unicode. isUppercase and isLowercase know about accented letters. For "é1É_" the lesson's code prints Uppercase: 1, Lowercase: 1, Digits: 1 and Symbols: 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 Int values, 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 print calls 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 let for a counter. The compiler rejects uppercase += 1 on 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" prints Uppercase: 1, Lowercase: 7, Digits: 1 and Symbols: 2.
    • "" prints zero on all four lines.
    • "é1É_" prints 1 on all four lines.
    Keep going with Swift1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free