Extend the length check with a second rule: the password must also contain at least one digit.
You start with:
The Rust editor starts empty, so write the whole program yourself. Begin with the data:
let password = "Secret99";
Build:
Length: 8 Has digit: True Strong
Rules:
- Print
"Length: "followed by the number of characters. - Loop through each character to set a boolean flag
has_digit, which starts asfalseand becomestruewhen a digit is seen. - Print
"Has digit: "followed by the flag. Write it asTrueorFalsewith a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself). - If the length is at least 8 and
has_digitistrue, printStrong; otherwise printWeak.
Try other passwords: with abcdefgh the output is
Length: 8 Has digit: False Weak
c.is_ascii_digit() works on a char.
let password = "Secret99";
let length = password.len();
println!("Length: {}", length);
let mut has_digit = false;
for character in password.chars() {
if character.is_ascii_digit() {
has_digit = true;
}
}
println!("Has digit: {}", if has_digit { "True" } else { "False" });
if length >= 8 && has_digit {
println!("Strong");
} else {
println!("Weak");
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
