Problem overview
The program checks a password for four kinds of character: an uppercase letter, a lowercase letter, a digit and a symbol. A symbol is any character that is neither a letter nor a digit. It prints the length, one line per kind (True or False) and a verdict. A password is Strong only if it has at least 8 characters and all four kinds. For Secret!99:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For secret99 the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak. The previous version checked one flag, the digit. This one tracks four, and its verdict is stricter. The helper boolText(b) from the previous lesson is available again; it returns "True" or "False".
Approach: one loop, four independent tests
All four flags can be filled in a single pass. For each character the loop asks four separate questions, and each question can set its own flag. The tests are four separate if statements, not an if/else if chain. A character belongs to at most one of the first three kinds, so a chain would work for those, but the symbol test is defined as "none of the above", and keeping every test independent makes each one read the same way.
The symbol test negates the other three. A character is a symbol when it is not in 'A' to 'Z', not in 'a' to 'z' and not in '0' to '9'. In code, the three ranges are joined with || inside one pair of parentheses, and a ! in front flips the result.
The verdict joins five conditions with &&, which Go evaluates left to right and stops at the first false one. The order doesn't change the result here, since the conditions only read variables. What matters is that all five are required: a password that misses even one of them falls through to the else and is Weak.
Step by step
- Store the length with
len(password)and print theLength:line, converting the number withstrconv.Itoa. - Create the four flags with
:=, each starting atfalse:hasUpper,hasLower,hasDigitandhasSymbol. Each means "not seen yet". - Loop with
for _, char := range password. The blank identifier discards the position, andcharis each character in turn. - Uppercase test. If
char >= 'A' && char <= 'Z', sethasUpper = true. - Lowercase test. The same shape with
'a'and'z'setshasLower. - Digit test. The range
'0'to'9'setshasDigit. - Symbol test. Wrap the three range tests in
||inside parentheses, put!in front, and sethasSymbol = truewhen the whole expression is true. - Print the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each through
boolText. - Write the verdict. Five conditions joined with
&&: the length and the four flags. PrintStrongif all hold. - Add the
elseon the closing-brace line, printingWeak.
The solution in Go
password := "Secret!99"
length := len(password)
fmt.Println("Length: " + strconv.Itoa(length))
hasUpper := false
hasLower := false
hasDigit := false
hasSymbol := false
for _, char := range password {
if char >= 'A' && char <= 'Z' {
hasUpper = true
}
if char >= 'a' && char <= 'z' {
hasLower = true
}
if char >= '0' && char <= '9' {
hasDigit = true
}
if !((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9')) {
hasSymbol = true
}
}
fmt.Println("Upper: " + boolText(hasUpper))
fmt.Println("Lower: " + boolText(hasLower))
fmt.Println("Digit: " + boolText(hasDigit))
fmt.Println("Symbol: " + boolText(hasSymbol))
if length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol {
fmt.Println("Strong")
} else {
fmt.Println("Weak")
}
Go details worth knowing here:
- Parentheses matter in the symbol test.
!binds tighter than||. Writing!(a) || (b) || (c)would negate only the first range. The outer parentheses make the!apply to the whole group of three. - Runes, not bytes.
rangeover a string yields runes (code points). A character such aséor a non-Latin letter is outside all three ranges, so this program counts it as a symbol. The length, fromlen, is a byte count, so a password with such characters is measured in bytes. - A space is a symbol. It is neither a letter nor a digit, so
Pass word1AgetsSymbol: Trueand isStrong. - Four independent
ifstatements. A flag is never reset, so each flag ends astrueif any character matched, whatever else the password contains. - Types. The flags are
booland the length isint.boolTextis needed because a bool can't be joined to a string with+either.
Edge cases and common mistakes
- All four kinds but short.
Ab1!printsTrueon all four lines and still getsWeak, because its length is 4. - Long but missing a symbol.
Secret99has 8 characters, an uppercase letter, lowercase letters and digits, but no symbol, soSymbol: FalseandWeak. - Forgetting the outer parentheses on the symbol test. The compiler accepts it, but the flag then comes out wrong, for instance
truefor every lowercase letter or digit. - Using
else ifbetween the tests. It happens to give the same flags for the first three tests here, but it ties the symbol test to the others in a way that is easy to break when the tests change. - Wrong output order. The lines must be Upper, Lower, Digit, Symbol, as the lesson's tests expect.
- Requiring only four conditions. The verdict needs five: the length and each of the four flags. Leaving out the length check would make
Ab1!Strong.
Testing
Secret!99prints all four flags asTrueandStrong.secret99printsUpper: False,Lower: True,Digit: True,Symbol: FalseandWeak.Secret99printsSymbol: FalseandWeak.Ab1!printsLength: 4, fourTruelines andWeak.Pass word1AprintsLength: 11, fourTruelines andStrong.
