C# Password Checker (constructive criticism wanted)

Hi all,
I’ve just finished the password check exercise and all in all I think it looks ok. However, I’d be interested to hear other opinions or how this could be optimised a bit?

PS: Nitpicking is welcome.

Thanks in advance, here’s the code:

using System;

namespace PasswordChecker
{
    class Program
    {
        public static void Main(string[] args)
        {

            //Define variables
            int minLength = 8;
            string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            string lowercase = "abcdefghijklmnopqrstuvwxyz";
            string digits = "0123456789";
            string specialChars = "!?£$%^&*:;@'~#{}[]/-_=+\"\"";

            //User input
            Console.WriteLine("Please enter a password: ");
            string passwordNew = Console.ReadLine();

            //Check password vs above variables to determine the score.
            int score = 0;
            if ((passwordNew == "1234") || (passwordNew == "password"))
            {
                score = 0;
            }
            else
            {
                if (passwordNew.Length >= minLength)
                {
                    score++;
                }
                if (Tools.Contains(passwordNew, uppercase))
                {
                    score++;
                }
                if (Tools.Contains(passwordNew, lowercase))
                {
                    score++;
                }
                if (Tools.Contains(passwordNew, digits))
                {
                    score++;
                }
                if (Tools.Contains(passwordNew, specialChars))
                {
                    score++;
                }

            }


            //Switch statement to return the users score.
            switch (score)
            {
                case 5:
                    Console.WriteLine("Extremely Strong");
                    break;

                case 4:
                    Console.WriteLine("Very Strong");
                    break;

                case 3:
                    Console.WriteLine("Strong");
                    break;

                case 2:
                    Console.WriteLine("Weak");
                    break;

                case 1:
                    Console.WriteLine("Very Weak");
                    break;

                default:
                    Console.WriteLine("Doesn't meet minimum requirements.");
                    break;
            }
        }
    }
}

Hello! It seems you’ve done a very good job! There isn’t much I would say to improve. You could think of having an array storing uppercase, lowercase, digits and specialChars's values:

string[] requirements = {"ABCetc", "abcetc", "123etc" "!?etc"};

Then you could loop through or simply check whether each requirement is met using indexing:

if (Tools.Contains(passwordNew, requirements[0]){//code}

But that is more of a subjective matter; you’ve done a very good job with keeping your code concise, easy to read and efficient-well done!

EDIT: I remembered that you. may not have done arrays yet-have you?

2 Likes

Hi,

Thanks for spending time going through my code and providing helpful feedback!

You’re quite right in your edit; I haven’t covered arrays just yet but it’s interesting to see how it could have been done differently :slight_smile:

1 Like