Start learning free

LESSON 197 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in Go: Four Character-Class Flags

Practice Read the full explanation

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) at false. Loop over every character and set the matching flag to true.
  • 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 as True or False with 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), print Strong; otherwise print Weak.
  • 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")
}

STEP 1 OF 13
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in Go, explained

    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

    1. Store the length with len(password) and print the Length: line, converting the number with strconv.Itoa.
    2. Create the four flags with :=, each starting at false: hasUpper, hasLower, hasDigit and hasSymbol. Each means "not seen yet".
    3. Loop with for _, char := range password. The blank identifier discards the position, and char is each character in turn.
    4. Uppercase test. If char >= 'A' && char <= 'Z', set hasUpper = true.
    5. Lowercase test. The same shape with 'a' and 'z' sets hasLower.
    6. Digit test. The range '0' to '9' sets hasDigit.
    7. Symbol test. Wrap the three range tests in || inside parentheses, put ! in front, and set hasSymbol = true when the whole expression is true.
    8. Print the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each through boolText.
    9. Write the verdict. Five conditions joined with &&: the length and the four flags. Print Strong if all hold.
    10. Add the else on the closing-brace line, printing Weak.

    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. range over 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, from len, 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 word1A gets Symbol: True and is Strong.
    • Four independent if statements. A flag is never reset, so each flag ends as true if any character matched, whatever else the password contains.
    • Types. The flags are bool and the length is int. boolText is needed because a bool can't be joined to a string with + either.

    Edge cases and common mistakes

    • All four kinds but short. Ab1! prints True on all four lines and still gets Weak, because its length is 4.
    • Long but missing a symbol. Secret99 has 8 characters, an uppercase letter, lowercase letters and digits, but no symbol, so Symbol: False and Weak.
    • Forgetting the outer parentheses on the symbol test. The compiler accepts it, but the flag then comes out wrong, for instance true for every lowercase letter or digit.
    • Using else if between 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!99 prints all four flags as True and Strong.
    • secret99 prints Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
    • Secret99 prints Symbol: False and Weak.
    • Ab1! prints Length: 4, four True lines and Weak.
    • Pass word1A prints Length: 11, four True lines and Strong.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free