Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?
You start with:
The Rust editor starts empty, so write the whole program yourself. Begin with the data:
let password = "Secret!99";
Build:
Length: 9 Upper: True Lower: True Digit: True Symbol: True Strong
Rules:
- Print
"Length: "followed by the number of characters. - Start four flags (
has_upper,has_lower,has_digit,has_symbol) atfalse. Loop over every character and set the matching flag totrue. - A symbol is any character that is not a letter and not a digit.
- Print one line per flag, in this order:
"Upper: ","Lower: ","Digit: ","Symbol: ", each 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 all four flags are
true(stricter than the last lesson), printStrong; otherwise printWeak.
Try other passwords: with secret99 the output is
Length: 8 Upper: False Lower: True Digit: True Symbol: False Weak
Use
c.is_ascii_uppercase(), c.is_ascii_lowercase(), c.is_ascii_digit(), c.is_ascii_alphanumeric().
let password = "Secret!99";
let length = password.len();
println!("Length: {}", length);
let mut has_upper = false;
let mut has_lower = false;
let mut has_digit = false;
let mut has_symbol = false;
for character in password.chars() {
if character.is_ascii_uppercase() {
has_upper = true;
}
if character.is_ascii_lowercase() {
has_lower = true;
}
if character.is_ascii_digit() {
has_digit = true;
}
if !character.is_ascii_alphanumeric() {
has_symbol = true;
}
}
println!("Upper: {}", if has_upper { "True" } else { "False" });
println!("Lower: {}", if has_lower { "True" } else { "False" });
println!("Digit: {}", if has_digit { "True" } else { "False" });
println!("Symbol: {}", if has_symbol { "True" } else { "False" });
if length >= 8 && has_upper && has_lower && has_digit && has_symbol {
println!("Strong");
} else {
println!("Weak");
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
