Start learning free

LESSON 193 OF 281 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in C++: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

string 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

isupper, islower, isdigit, isalnum with ++.

string password = "Hello!World9";

int uppercase = 0;
int lowercase = 0;
int digits = 0;
int symbols = 0;
for (char c : password) {
    if (isupper(c)) {
        uppercase++;
    }
    if (islower(c)) {
        lowercase++;
    }
    if (isdigit(c)) {
        digits++;
    }
    if (!isalnum(c)) {
        symbols++;
    }
}
cout << "Uppercase: " << uppercase << endl;
cout << "Lowercase: " << lowercase << endl;
cout << "Digits: " << digits << endl;
cout << "Symbols: " << symbols << endl;

STEP 1 OF 13
    Ctrl + Enter to check

    C++ · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in C++, explained

    Problem overview

    The previous version only asked whether each kind of character was present. This one asks how many of each there are. The program prints four lines: the number of uppercase letters, lowercase letters, digits and symbols in the password, where a symbol is any character that is neither a letter nor a digit.

    For Hello!World9 the output is:

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

    There is no verdict line in this version: the task is the counting itself.

    Approach: swap each flag for a counter

    A flag remembers "seen or not". A counter remembers "how many times". The loop has the same shape as before, one pass over the characters with one test per class, but each test now adds 1 to an int that started at 0 instead of setting a bool to true.

    The four counters are independent, and each character matches exactly one of the four tests, so after the pass the four numbers add up to the length of the password.

    Step by step

    1. Create uppercase as an int set to 0.
    2. Create lowercase the same way.
    3. Create digits the same way.
    4. Create symbols the same way. A space counts as a symbol, because it is neither a letter nor a digit.
    5. Open a range-based for with char c over password.
    6. If isupper(c), do uppercase++.
    7. If islower(c), do lowercase++.
    8. If isdigit(c), do digits++.
    9. If !isalnum(c), do symbols++.
    10. After the loop, print the Uppercase line: the label, the counter and endl. cout prints an int as digits.
    11. Print the Lowercase, Digits and Symbols lines the same way, in that order.

    The solution in C++

    string password = "Hello!World9";
    
    int uppercase = 0;
    int lowercase = 0;
    int digits = 0;
    int symbols = 0;
    for (char c : password) {
        if (isupper(c)) {
            uppercase++;
        }
        if (islower(c)) {
            lowercase++;
        }
        if (isdigit(c)) {
            digits++;
        }
        if (!isalnum(c)) {
            symbols++;
        }
    }
    cout << "Uppercase: " << uppercase << endl;
    cout << "Lowercase: " << lowercase << endl;
    cout << "Digits: " << digits << endl;
    cout << "Symbols: " << symbols << endl;
    

    Details worth a closer look:

    • Counters start at zero. A local int that is declared without a value holds an unspecified number. The = 0 is what makes the counts correct.
    • x++ means add 1. It is shorthand for x = x + 1. Used as a statement on its own, as here, it only increases the variable.
    • The printing is outside the loop. The four lines run once, after every character has been counted. Placed inside the loop, they would print the running counts twelve times.
    • Four ifs, not an else if chain. A character falls into at most one class, so a chain would give the same counts here. Separate if statements keep each rule readable on its own.
    • Symbols by elimination. !isalnum(c) is true for anything that is not a letter or a digit, including punctuation and spaces.
    • Non-ASCII input. isupper and the other functions expect a value that fits in an unsigned char. A plain char holding a byte above 127 is usually negative, which makes the call undefined behaviour; the safe form is isupper((unsigned char)c). The lesson's passwords are ASCII.

    Following Hello!World9

    The password has twelve characters. The H and the W bump uppercase, the letters ello and orld bump lowercase eight times, the ! bumps symbols and the final 9 bumps digits. That gives 2, 8, 1 and 1, and the four numbers add up to 12, the password's length. If the sum is ever different from the length, a character was missed or counted twice.

    Counters contain the flags

    A counter carries more information than a flag. The flag hasDigit from the earlier versions is the same fact as the condition digits > 0, so a program with these four counters can still answer every yes-or-no question from before. The reverse is not true: a flag cannot be turned back into a count. That is why counting is the more general tool, and why it costs nothing extra here: the loop is the same length and does the same number of tests. The only difference is the statement in each branch: digits++ instead of hasDigit = true.

    Complexity

    Time: O(n) for n characters, each tested a constant number of times. Space: O(1): four integers, however long the password is.

    Edge cases and common mistakes

    • Spaces. Pass word!2 has the space and the !, so Symbols is 2, not 1. The output is Uppercase: 1, Lowercase: 7, Digits: 1, Symbols: 2.
    • An empty password. The loop never runs, and all four lines print 0.
    • A missing initialiser. Without = 0 a counter starts from an unspecified value and the output is wrong or varies between runs.
    • Counting with a flag. Setting a bool instead of incrementing would say whether a class exists, but not how many characters belong to it.
    • Printing inside the loop. It repeats the lines once per character.
    • Wrong line order. The output is Uppercase, Lowercase, Digits, Symbols.

    Testing

    passwordUppercaseLowercaseDigitsSymbols
    Hello!World92811
    Pass word!21712
    (empty string)0000

    The next version builds on this program.

    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free