Problem overview
This version counts instead of flagging. For Hello!World9, the program reports how many uppercase letters, lowercase letters, digits and symbols the password has:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
A symbol is any character that is not a letter and not a digit, so a space counts as one. With Pass word!2 the output is Uppercase: 1, Lowercase: 7, Digits: 1 and Symbols: 2: the space and the exclamation mark are the two symbols.
Approach: four counters in one pass
A flag answers "is there at least one?" A counter answers "how many?" The change from the previous version is small: where a flag was set to true, a counter goes up by one. Each counter starts at 0, and a single loop over the password adds 1 to the right counter for each character.
Each character belongs to exactly one of the four classes. Uppercase, lowercase and digit are tested by range. The symbol test is true when none of the three ranges matches. The counts therefore add up to the number of characters, which is a useful check on the output.
The four counts give you a check on your own work. For a password made only of ASCII characters, the counts add up to the password's length: for Hello!World9 that is 2 + 8 + 1 + 1 = 12, and the password has 12 characters. For Pass word!2 it is 1 + 7 + 1 + 2 = 11. If your totals come out different, one of the conditions overlaps another or misses a case.
Counters also hold more information than flags. A flag can only say that a class is present; a count says how much of it there is, so a rule like "at least two digits" becomes possible. This version only reports the counts and gives no Strong or Weak verdict, so the output is the four numbers and nothing else.
Step by step
- Create the four counters with
:=, each set to0:uppercase,lowercase,digitsandsymbols. Go infersintfrom the literal. - Start the loop.
for _, c := range passwordvisits each character asc;_throws away the position. - Count uppercase. When
c >= 'A' && c <= 'Z', runuppercase++. - Count lowercase with the range
'a'to'z'. - Count digits with the range
'0'to'9'. - Count symbols. The condition is the negation of the three ranges together,
!(... || ... || ...), so every other character, including spaces and punctuation, adds tosymbols. - Print four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each joins its label to the counter, converted with
strconv.Itoa, since Go does not join anintto a string with+.
The solution in Go
password := "Hello!World9"
uppercase := 0
lowercase := 0
digits := 0
symbols := 0
for _, c := range password {
if c >= 'A' && c <= 'Z' {
uppercase++
}
if c >= 'a' && c <= 'z' {
lowercase++
}
if c >= '0' && c <= '9' {
digits++
}
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
symbols++
}
}
fmt.Println("Uppercase: " + strconv.Itoa(uppercase))
fmt.Println("Lowercase: " + strconv.Itoa(lowercase))
fmt.Println("Digits: " + strconv.Itoa(digits))
fmt.Println("Symbols: " + strconv.Itoa(symbols))
Go details worth knowing here:
++is a statement.uppercase++adds 1 to the variable, but in Go it is a statement, not an expression. You can't writex := uppercase++or use it inside a larger expression, and there is no prefix form++uppercase.- Four independent
ifstatements. They don't needelse, because the ranges don't overlap. The symbol condition covers whatever the other three leave out. - Counting runes.
rangeover a string yields one rune per character, even for multi-byte characters. The string"Ünï1"has two accented letters, which fall outside'A'to'Z'and'a'to'z', so the program reportsUppercase: 0,Lowercase: 1,Digits: 1andSymbols: 2. The two accented letters are counted as symbols, once each, not twice for their two bytes. - Unused variables are errors. Each counter is read in a
Println, so all four are used. Declaring a counter and never reading it would stop the build.
Edge cases and common mistakes
- An empty password. The loop never runs, and all four lines show 0.
- Spaces. A space is not a letter or a digit, so it is a symbol.
Pass word!2has two symbols. - Using
else iffor the symbol case. That works only if the chain is complete and in the right order. Four independentifstatements are harder to get wrong. - Dropping the outer parentheses in the symbol test. Since
!binds tighter than||,!(a) || (b) || (c)negates only the first range, and the count comes out wrong. - Counting once per class instead of per character. Setting a counter to 1 instead of adding 1 is a flag, not a count.
- Forgetting to convert.
"Uppercase: " + uppercasedoes not compile. Usestrconv.Itoa(uppercase).
Testing
Hello!World9prints 2, 8, 1 and 1.Pass word!2prints 1, 7, 1 and 2.- An empty string prints 0 on all four lines.
Ünï1prints 0, 1, 1 and 2, since the accented letters count as symbols.
