Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?
You start with:
password := "Secret!99"
Build:
Length: 9 Upper: True Lower: True Digit: True Symbol: True Strong
Rules:
- Print
"Length: "followed by the number of characters. - Start four flags (
hasUpper,hasLower,hasDigit,hasSymbol) atfalse. Loop over every character and set the matching flag totrue. - A symbol is any character that is not a letter and not a digit.
- Print one line per flag, in this order:
"Upper: ","Lower: ","Digit: ","Symbol: ", each followed by the flag. Write it asTrueorFalsewith a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself). - If the length is at least 8 and all four flags are
true(stricter than the last lesson), printStrong; otherwise printWeak. - Use
strconv.Itoa(n)to turn a number into text before joining it to a string. - The helper
boolText(b)returns"True"or"False"for a boolean.
Try other passwords: with secret99 the output is
Length: 8 Upper: False Lower: True Digit: True Symbol: False Weak
Use ASCII range comparisons:
c >= 'A' && c <= 'Z' for uppercase, c >= 'a' && c <= 'z' for lowercase, c >= '0' && c <= '9' for digits, and the negation of all three for symbols.
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")
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
