Start learning free

LESSON 199 OF 671 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 - Count Character Classes

Practice

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