Start learning free

LESSON 198 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in Go: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

password := "Hello!World9"

Build:

Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1

Rules:

  • Use four integer counters, all starting at 0.
  • Loop through each character and add 1 to the matching counter.
  • Treat any character that is not a letter and not a digit as a symbol.
  • Print one line per counter, in this order: "Uppercase: ", "Lowercase: ", "Digits: ", "Symbols: ", each followed by the count.
  • Use strconv.Itoa(n) to turn a number into text before joining it to a string.

Try other passwords: with Pass word!2 the output is

Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2

ASCII range comparisons c >= 'A' && c <= 'Z' etc. with ++.

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))

STEP 1 OF 13
    Ctrl + Enter to check

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in Go, explained

    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

    1. Create the four counters with :=, each set to 0: uppercase, lowercase, digits and symbols. Go infers int from the literal.
    2. Start the loop. for _, c := range password visits each character as c; _ throws away the position.
    3. Count uppercase. When c >= 'A' && c <= 'Z', run uppercase++.
    4. Count lowercase with the range 'a' to 'z'.
    5. Count digits with the range '0' to '9'.
    6. Count symbols. The condition is the negation of the three ranges together, !(... || ... || ...), so every other character, including spaces and punctuation, adds to symbols.
    7. 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 an int to 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 write x := uppercase++ or use it inside a larger expression, and there is no prefix form ++uppercase.
    • Four independent if statements. They don't need else, because the ranges don't overlap. The symbol condition covers whatever the other three leave out.
    • Counting runes. range over 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 reports Uppercase: 0, Lowercase: 1, Digits: 1 and Symbols: 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!2 has two symbols.
    • Using else if for the symbol case. That works only if the chain is complete and in the right order. Four independent if statements 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: " + uppercase does not compile. Use strconv.Itoa(uppercase).

    Testing

    • Hello!World9 prints 2, 8, 1 and 1.
    • Pass word!2 prints 1, 7, 1 and 2.
    • An empty string prints 0 on all four lines.
    • Ünï1 prints 0, 1, 1 and 2, since the accented letters count as symbols.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free