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
- Create four
intcounters,uppercase,lowercase,digitsandsymbols, all set to 0. A counter must start at zero before anything is counted. - Loop over the characters with
foreach (char c in password). For this password the body runs 12 times. - Count uppercase letters. If
char.IsUpper(c)is true, runuppercase++. - Count lowercase letters the same way with
char.IsLower(c). - Count digits with
char.IsDigit(c). - Count symbols with
!char.IsLetterOrDigit(c): the test is true for everything that is not a letter and not a digit. - 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 foruppercase = uppercase + 1. As a statement on its own line, the prefix form++uppercasedoes 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 writinguppercase++;is a compile error: Use of unassigned local variable. That is why each counter is set to0in its declaration. - Joining an
intto text. In"Digits: " + digits, C# converts the number to text because the other operand is astring. Nothing needs to be formatted by hand. - An
intis 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 threelcharacters 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 separateifstatements 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"givesUppercase: 2,Lowercase: 8,Digits: 1,Symbols: 1."Pass word!2"givesUppercase: 1,Lowercase: 7,Digits: 1,Symbols: 2."ALL CAPS 123"givesUppercase: 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.
