0

Password Validation

I failed with Test Case #10 and Test Case #11. Can someone help me? This is my code: namespace Program { class Program { static void Main(string[] args) { string password = Console.ReadLine(); Password p = new Password(); p.CheckPassword(password); } } public class Password { public List<string> speCharacters = new List<string>() { "!", "@", "#", "

quot;, "%", "&", "*", }; public void CheckPassword(string password) { int count = 0; int length = 0; if (password.Length > 7) { foreach (string s in speCharacters) { if (password.Contains(s)) { count++; } } foreach (int num in password) { length++; } if (count >= 2 && length >= 2) { Console.WriteLine("Strong"); } else { Console.WriteLine("Weak"); } } else { Console.WriteLine("Weak"); } } } } Is anything wrong? Help me.

28th Nov 2023, 2:22 AM
Lou Vinh Luan
7 ответов
+ 7
Lou , there are still issues in the code: > the iteration and check for *special characters* is still not the way it should be done. we have to iterate over the password and compare if the current character is in the special characters list. (see the comment in my last answer) > the counting of digits is not working properly. even if the password does not contain any digits, it will pass. so if we have: `abcde@#`, it is giving `Strong`, and the counter shows a number that equals the number of total characters in the pass word.
28th Nov 2023, 8:32 PM
Lothar
Lothar - avatar
+ 6
Lou , there is an issue with counting the required characters. e.g.: your code is iterating over the list of special characters, then incrementing the counter. this will not working properly when a special character is used *multiples times* in a password. password: `12&#abcx` will count correctly => 2 digits, 2 special chars password: `11&&abcx` will NOT count correctly => 2 digit, 1 special char to avoid this issue it is required to iterate over the password and do the counting this way.
28th Nov 2023, 7:46 AM
Lothar
Lothar - avatar
+ 5
Lou , the password you mentioned in your last post is *weak*, because it has no numerical values.
29th Nov 2023, 9:06 PM
Lothar
Lothar - avatar
+ 1
Please use the share button in the Code Playground to post your code.
28th Nov 2023, 5:46 AM
Keith
Keith - avatar
+ 1
Lothar , > I can not visualize the code to iterate over the password. Can you give me your code, please? > I tried the password ‘abcde@#’ and it was still giving ‘Weak’. Is it right? Thank you so much.
29th Nov 2023, 12:44 AM
Lou Vinh Luan
28th Nov 2023, 6:04 AM
Lou Vinh Luan
0
Bro, I have fixed my code and the Test Case #10, #11 completed. But a new problem came, Test Case #7 failed. What's wrong? Here is my code after fixing: https://www.sololearn.com/compiler-playground/c2nxdAumhmns
28th Nov 2023, 1:06 PM
Lou Vinh Luan