Problem overview
The checker now counts instead of only detecting. For a password, the program reports how many uppercase letters, lowercase letters, digits and symbols it contains. A symbol is any character that is not a letter and not a digit, and that includes a space. The earlier version used four boolean flags that said whether each class was present; this one uses four int counters that say how many.
For Hello!World9 the output is:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
For Pass word!2, which contains a space and an exclamation mark:
Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2
Approach: four counters, one pass
A counter is an int that starts at 0 and goes up by one each time a matching character is found. The structure is the same as with flags: one loop over the characters, and one if per class inside it. The only change is the action. Instead of hasUpper = true the code runs uppercase++.
That small change loses nothing: a flag can be derived from a counter, because a class is present exactly when its counter is above 0. A counter keeps more information than a flag.
The four tests are independent if statements. A character that is an uppercase letter satisfies only the first, a digit only the third, and a space only the last, so every character adds to exactly one counter. That gives a useful check: the four counters always add up to the length of the password.
Step by step
- Create the four counters one by one:
uppercase,lowercase,digitsandsymbols, allintvariables set to 0. Each class has its own counter. - Start the loop. The
charvariablectakes each character ofpassword.toCharArray(), once per character, 12 times forHello!World9. - Uppercase. When
Character.isUpperCase(c)is true, runuppercase++. - Lowercase. When
Character.isLowerCase(c)is true, runlowercase++. - Digits. When
Character.isDigit(c)is true, rundigits++. - Symbols. When
!Character.isLetterOrDigit(c)is true, runsymbols++. Letters and digits make this false; everything else, a space included, counts. - Print the four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to its counter with
+.
The solution in Java
String password = "Hello!World9";
int uppercase = 0;
int lowercase = 0;
int digits = 0;
int symbols = 0;
for (char c : password.toCharArray()) {
if (Character.isUpperCase(c)) {
uppercase++;
}
if (Character.isLowerCase(c)) {
lowercase++;
}
if (Character.isDigit(c)) {
digits++;
}
if (!Character.isLetterOrDigit(c)) {
symbols++;
}
}
System.out.println("Uppercase: " + uppercase);
System.out.println("Lowercase: " + lowercase);
System.out.println("Digits: " + digits);
System.out.println("Symbols: " + symbols);
The Java details worth a closer look:
++.uppercase++adds 1 to the variable. It is shorthand foruppercase = uppercase + 1. As a statement on its own,++uppercasedoes the same thing.- Counters must start at a value. A local variable that is declared but not assigned cannot be read.
int uppercase;followed byuppercase++does not compile, because the compiler says the variable might not have been initialized. That is why every counter is set to 0 up front. - Four
ifs, not anelsechain. The symbol test is a negative one, defined as "not a letter and not a digit". It does not need to know which class a letter belongs to, so the tests stay independent of each other. - Wide characters. The loop works on UTF-16 units, so a character outside the basic range, such as an emoji, is two units. Each unit is tested separately and neither is a letter or digit, so such a character adds 2 to
symbols. - Naming. The counter
digitsand the methodCharacter.isDigitare unrelated names. The variable is just anint.
Complexity
Time: O(n) for a password of n characters, with four constant-time tests per character. Space: O(n), because toCharArray() copies the characters into a new array; the counters themselves take constant space.
Edge cases and common mistakes
- Empty password. The loop never runs and all four counters print 0.
- Spaces. A space is not a letter or a digit, so it counts as a symbol. In
Pass word!2the space and the!makeSymbols: 2. - Counting the total. In
Hello!World9, 2 + 8 + 1 + 1 equals 12, the length. If your four counters do not add up to the length, a character was counted twice or not at all. - Forgetting to start at 0. The compiler rejects an uninitialized counter, as described above.
- Printing inside the loop. Each line would be printed once per character. The printing code belongs after the loop.
- Wrong order of the output lines. The lines must appear as Uppercase, Lowercase, Digits, Symbols, with the exact labels, including the plural
DigitsandSymbols. - Using
=instead of++.uppercase = 1would only remember that something was found, which is a flag again, not a count.
Testing
Hello!World9gives 2, 8, 1 and 1.Pass word!2gives 1, 7, 1 and 2.- The empty string gives 0, 0, 0 and 0.
