Start learning free

LESSON 200 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in Go: A Score from 0 to 100

Practice Read the full explanation

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

    GO · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in Go, explained

    Problem overview

    This version turns the password rules into a score. Starting from 0, the program adds points for each property the password has:

    • 25 points for a length of at least 8;
    • 20 points for an uppercase letter;
    • 20 points for a lowercase letter;
    • 20 points for a digit;
    • 15 points for a symbol, meaning any character that is not a letter or a digit.

    The five amounts add up to 100. The program prints the length, then the score. For Secret!99:

    Length: 9
    Score: 100

    For abc123 the output is Length: 6 and Score: 40: the lowercase letters give 20 and the digits give 20, and the length is under 8.

    Approach: collect the facts, then add up the points

    The work splits into two stages. The first stage is a single loop over the password that sets four flags: hasUpper, hasLower, hasDigit and hasSymbol. The second stage turns facts into points: one if per rule, each adding its own amount to score.

    Keeping the stages apart makes each easy to follow. The loop knows nothing about points, and the scoring knows nothing about characters. The rules use independent if statements, never else if, because a password can earn several awards at once.

    The weights are chosen so that a password with every property reaches exactly 100, and no more. That is why no extra code is needed to cap the score: each rule can fire at most once, so the highest possible total is 25 + 20 + 20 + 20 + 15. Each property is counted once however many characters of that kind there are. A password with ten digits earns the same 20 points as one with a single digit.

    The earlier versions printed each fact and a verdict. This one reduces everything to one number, which is easy to compare between passwords but hides which rule was missed. The Length: line is printed to give some context for the score.

    Step by step

    1. Store the length. length := len(password) gives the number of bytes in the string, which is the number of characters for ASCII text. It is used for the score and for the first output line.
    2. Create four flags, all false: hasUpper, hasLower, hasDigit, hasSymbol.
    3. Loop with for _, c := range password. The blank identifier discards the position, and c is each character.
    4. Set the flags. Three range tests handle 'A' to 'Z', 'a' to 'z' and '0' to '9'. The fourth test is true when c is in none of the three ranges.
    5. Create score and set it to 0, before any rule is checked.
    6. Award the length points. if length >= 8 runs score += 25. The operator += adds to the variable in place.
    7. Award the class points. One if each for hasUpper, hasLower and hasDigit adds 20, and hasSymbol adds 15.
    8. Print the result. The Length: line and the Score: line each join a label to a number converted with strconv.Itoa, because Go does not join an int to a string with +.

    The solution in Go

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

    Go details worth knowing here:

    • len counts bytes. For ASCII passwords that equals the number of characters. For a string with accented or non-Latin letters, len is larger, which affects the length rule. The string "héllo" has 5 characters and len gives 6.
    • range yields runes. c is a rune, so the character literals 'A', 'z' and '9' compare with it directly. A character outside the ASCII ranges, such as é, passes the "none of the three" test and counts as a symbol.
    • Compound assignment. score += 25 means score = score + 25.
    • Conditions on booleans. if hasUpper needs no == true.
    • Order of statements. The scoring if statements must come after the loop. Before it, the flags would still be false.

    Edge cases and common mistakes

    • Using else if for the rules. Only the first matching rule would score. Secret!99 would get 25 instead of 100.
    • An empty password. The loop does not run, no rule matches, and the output is Length: 0 and Score: 0.
    • Short but varied. Aa1! has all four classes but only 4 characters: 20 + 20 + 20 + 15 is 75, and the length adds nothing.
    • Long but one class. abcdefgh scores 45: 25 for the length and 20 for lowercase letters. !!!!!!!! scores 40: 25 plus 15.
    • Printing before scoring. The output lines come after the points are added, so they show the final values.
    • Misreading the points. The class points differ: a symbol is worth 15 and the others 20. Using 20 for all of them would give 105 as the maximum, past the 100 the lesson aims for.

    Testing

    • Secret!99 prints Length: 9 and Score: 100.
    • abc123 prints Length: 6 and Score: 40.
    • abcdefgh prints Length: 8 and Score: 45.
    • !!!!!!!! prints Length: 8 and Score: 40.
    • Aa1! prints Length: 4 and Score: 75.
    • An empty string prints Length: 0 and Score: 0.
    Keep going with Go1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free