Count how many characters of each class the password contains.
You start with:
The Rust editor starts empty, so write the whole program yourself. Begin with the data:
let password = "Hello!World9";
Build:
Uppercase: 2 Lowercase: 8 Digits: 1 Symbols: 1
Rules:
- Use four integer counters, all starting at 0.
- Loop through each character and add 1 to the matching counter.
- Treat any character that is not a letter and not a digit as a symbol.
- Print one line per counter, in this order:
"Uppercase: ","Lowercase: ","Digits: ","Symbols: ", each followed by the count.
Try other passwords: with Pass word!2 the output is
Uppercase: 1 Lowercase: 7 Digits: 1 Symbols: 2
c.is_ascii_uppercase(), c.is_ascii_lowercase(), c.is_ascii_digit(), c.is_ascii_alphanumeric() with += 1.
let password = "Hello!World9";
let mut uppercase = 0;
let mut lowercase = 0;
let mut digits = 0;
let mut symbols = 0;
for character in password.chars() {
if character.is_ascii_uppercase() {
uppercase += 1;
}
if character.is_ascii_lowercase() {
lowercase += 1;
}
if character.is_ascii_digit() {
digits += 1;
}
if !character.is_ascii_alphanumeric() {
symbols += 1;
}
}
println!("Uppercase: {}", uppercase);
println!("Lowercase: {}", lowercase);
println!("Digits: {}", digits);
println!("Symbols: {}", symbols);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
