I wrote some Python code to separate numbers, uppercase letters, lowercase letters and special characters. password_chars
is a list where each index is a character from a string that I inputted and this code is in a for loop, each index is the for loop is a single character in a loop. The numbers being increased by one are keeping count of the number of lowercase letters, uppercase letters and special characters:
try:
int(password_input[i])
numbers += 1
except ValueError:
if password_chars[i] != password_chars[i].lower():
capital_letters += 1
elif password_chars[i] != password_chars[i].upper():
lowercase_letters += 1
else:
special_chars += 1
I also wrote some C++ code do do the same (character
is an index of an array made from a string, and this is also in a for loop):
try {
int temp = stoi(character);
numbers_index ++;
}
catch(std::invalid_argument& e) {
if(character >= "A" && character <= "Z") {
capital_letters_index ++;
} else if(character >= "a" && character <= "z") {
lowercase_letters_index ++;
} else {
special_chars_index ++;
}
}
I have successfully written a simple Rust way to separate numbers from the rest (character
is an index of a vector, this is also in a for loop):
match character.to_string().parse::<u8>() {
Ok(_) => {
numbers_index += 1;
}
Err(_e) => {
// other sorters go here
}
}
but I have been unable to find a way to separate them. The main solution I have been working on is:
match character.to_string().to_uppercase() {
Ok(..) => {
capital_letters_index += 1;
}
Err(..) => {
match character.to_string().to_lowercase() {
Ok(..) => {
lowercase_letters_index += 1;
}
Err(..) => {
special_chars_index += 1;
}
}
}
}
but I get this compile error:
error[E0308]: mismatched types
--> src\main.rs:43:29
|
39 | match character.to_string().to_lowercase() {
| ------------------------------------ this expression has type `String`
...
43 | Err(..) => {
| ^^^^^^^ expected struct `String`, found enum `std::result::Result`
|
= note: expected struct `String`
found enum `std::result::Result<_, _>`
and after a lot of looking up and a few days of looking at it, I cannot figure out how to fix this. I am completely open to other ways of counting these, this is just the easiest one I could think of.