Combine the earlier checks into a single numeric score between 0 and 100.
You start with:
The Rust editor starts empty, so write the whole program yourself. Begin with the data:
let mut password = "Secret!99";
Build:
Length: 9 Score: 100
Rules:
- Start with
score = 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.
Try other passwords: with abc123 the output is
Length: 6 Score: 40
Use the same char methods, then accumulate into a mutable integer
score.
let mut password = "Secret!99";
let mut length = password.len();
let mut hasUpper = false;
let mut hasLower = false;
let mut has_digit = false;
let mut hasSymbol = false;
for c in password.chars() {
if c.is_uppercase() {
hasUpper = true;
}
if c.is_lowercase() {
hasLower = true;
}
if c.is_ascii_digit() {
has_digit = true;
}
if !c.is_alphanumeric() {
hasSymbol = true;
}
}
let mut score = 0;
if length >= 8 {
score += 25;
}
if hasUpper {
score += 20;
}
if hasLower {
score += 20;
}
if has_digit {
score += 20;
}
if hasSymbol {
score += 15;
}
println!("Length: {}", length);
println!("Score: {}", score);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
