Start learning free

LESSON 197 OF 289 · 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

char.IsUpper, char.IsLower, char.IsDigit, char.IsLetterOrDigit, with ++ counters.

string password = "Hello!World9";

int uppercase = 0;
int lowercase = 0;
int digits = 0;
int symbols = 0;
foreach (char c in password) {
    if (char.IsUpper(c)) {
        uppercase++;
    }
    if (char.IsLower(c)) {
        lowercase++;
    }
    if (char.IsDigit(c)) {
        digits++;
    }
    if (!char.IsLetterOrDigit(c)) {
        symbols++;
    }
}
Console.WriteLine("Uppercase: " + uppercase);
Console.WriteLine("Lowercase: " + lowercase);
Console.WriteLine("Digits: " + digits);
Console.WriteLine("Symbols: " + symbols);

STEP 1 OF 13
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in C#, explained

    Problem overview

    The earlier versions only asked whether a password contains a kind of character. This one asks how many of each kind it contains. The program counts uppercase letters, lowercase letters, digits and symbols, and prints one line per count in that order.

    For "Hello!World9" the output is:

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

    The uppercase letters are H and W. The lowercase letters are ello and orld, eight in all. 9 is the only digit and ! is the only symbol. A symbol is any character that is not a letter and not a digit, so a space counts as one. For "Pass word!2" the counts are 1, 7, 1 and 2: the space and the ! are both symbols.

    Approach: four counters and one loop

    A flag remembers a yes or no. A counter remembers a number: it starts at 0 and goes up by one each time a matching character is seen. The structure is the one from the flag lessons, with int variables in place of bool ones and ++ in place of = true.

    Each class gets its own counter, and the loop visits every character once. Inside the loop, four separate if statements each test one class and increase the matching counter. The counters are only printed after the loop, when every character has been counted. Printing inside the loop would show partial counts.

    The four tests overlap on nothing: a character cannot be both a digit and an uppercase letter, so each character increases at most one counter.

    Step by step

    1. Create four int counters, uppercase, lowercase, digits and symbols, all set to 0. A counter must start at zero before anything is counted.
    2. Loop over the characters with foreach (char c in password). For this password the body runs 12 times.
    3. Count uppercase letters. If char.IsUpper(c) is true, run uppercase++.
    4. Count lowercase letters the same way with char.IsLower(c).
    5. Count digits with char.IsDigit(c).
    6. Count symbols with !char.IsLetterOrDigit(c): the test is true for everything that is not a letter and not a digit.
    7. Print the four lines after the loop, each as a label joined to its counter with +, in the order Uppercase, Lowercase, Digits, Symbols.

    The solution in C#

            string password = "Hello!World9";
    
            int uppercase = 0;
            int lowercase = 0;
            int digits = 0;
            int symbols = 0;
            foreach (char c in password) {
                if (char.IsUpper(c)) {
                    uppercase++;
                }
                if (char.IsLower(c)) {
                    lowercase++;
                }
                if (char.IsDigit(c)) {
                    digits++;
                }
                if (!char.IsLetterOrDigit(c)) {
                    symbols++;
                }
            }
            Console.WriteLine("Uppercase: " + uppercase);
            Console.WriteLine("Lowercase: " + lowercase);
            Console.WriteLine("Digits: " + digits);
            Console.WriteLine("Symbols: " + symbols);
    

    The C# details worth a closer look:

    • ++ adds one. uppercase++ is short for uppercase = uppercase + 1. As a statement on its own line, the prefix form ++uppercase does the same thing.
    • Counters need a starting value. C# refuses to read a local variable that has never been assigned. Declaring int uppercase; and then writing uppercase++; is a compile error: Use of unassigned local variable. That is why each counter is set to 0 in its declaration.
    • Joining an int to text. In "Digits: " + digits, C# converts the number to text because the other operand is a string. Nothing needs to be formatted by hand.
    • An int is plenty. It holds values up to about 2.1 billion, far beyond any password length.

    Check the counts against the length

    For ordinary ASCII text, every character falls into exactly one of the four classes, so the four counters add up to password.Length. In the example, 2 + 8 + 1 + 1 is 12, the length of "Hello!World9". When the counts don't add up, a test is wrong. The sum can fall short for text with letters that have no case, such as '中': char.IsLetterOrDigit is true for it, so it is not a symbol, but it is neither upper nor lower either. It can also exceed the length when an emoji is present: an emoji takes two char values, and each counts as a symbol. Neither case arises with the passwords in this lesson.

    Edge cases and common mistakes

    • Repeated characters count every time. "Hello!World9" has three l characters and all three are counted. The counters measure occurrences, not different characters.
    • Spaces are symbols. "Pass word!2" reports 2 symbols, the space and the !.
    • An empty password. "" never enters the loop, so all four lines show 0.
    • Forgetting to start at 0. The compiler catches this, as described above, rather than printing garbage.
    • Using else if. It works for these four exclusive tests, but separate if statements keep each test independent, and they are what the lesson uses.
    • Putting the printing inside the loop. The four lines would be printed once per character, showing the counts growing.
    • Swapping the counters in the output. The lines must read Uppercase, Lowercase, Digits, Symbols, in that order.

    Testing

    • "Hello!World9" gives Uppercase: 2, Lowercase: 8, Digits: 1, Symbols: 1.
    • "Pass word!2" gives Uppercase: 1, Lowercase: 7, Digits: 1, Symbols: 2.
    • "ALL CAPS 123" gives Uppercase: 7, Lowercase: 0, Digits: 3, Symbols: 2. The two spaces are the symbols, and a zero count is printed like any other.
    • "" gives four lines with a count of 0.
    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