Start learning free

LESSON 173 OF 404 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 - Scored 0-100 in Go

Practice

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

password := "Secret!99"

Build:

Length: 9
Score: 100

Rules:

  • Start the score at 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.
  • Use strconv.Itoa(n) to turn a number into text before joining it to a string.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Use character range checks (e.g. c >= 'A' && c <= 'Z'), then accumulate into score.

password := "Secret!99"

length := len(password)
hasUpper := false
hasLower := false
hasDigit := false
hasSymbol := false
for _, c := range password {
    if c >= 'A' && c <= 'Z' {
        hasUpper = true
    }
    if c >= 'a' && c <= 'z' {
        hasLower = true
    }
    if c >= '0' && c <= '9' {
        hasDigit = true
    }
    if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
        hasSymbol = true
    }
}
score := 0
if length >= 8 {
    score += 25
}
if hasUpper {
    score += 20
}
if hasLower {
    score += 20
}
if hasDigit {
    score += 20
}
if hasSymbol {
    score += 15
}
fmt.Println("Length: " + strconv.Itoa(length))
fmt.Println("Score: " + strconv.Itoa(score))

STEP 1 OF 18
    Ctrl + Enter to check

    Password Strength V6 in Go

    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free