C# Password Check Exercise

Hi,

I am pulling my hair out with this exercise because if the hints still don’t make sense, then you cant continue unless you check all the boxes, which then enables you to move on however, without knowing the solution for that previous exercise! The step by step ‘Run’ method is much better, but even still, if you press ‘Give me the solution’, it hands out the WHOLE solution of the exercise, which is unhelpful because I didnt get to practice it step by step…Can you please email the full solution code for the C# Password Checker exercise, otherwise I cannot continue. Thanks.

4 Likes

Why would you need the full solution mailed if you can get it with solution button? You can always copy your code first to make a back-up.

if you want help in steps, you can come to the forum. See this topic:

How to ask good questions (and get good answers)

to see how to make good posts (to get good replies)

1 Like

I’m having a mistake with the password as in when I put in my password:
thatsallfolks
It says the score is 1 while the hint says the score will come out as 2.
Why is this?

Would have to see your code in order to help.

exercise url: https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-password-checker

using System;

namespace PasswordChecker
{
  class Program
  {
    public static void Main(string[] args)
    {
      int minLength = 8;
      string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      string lowercase = "abcdefghijklmnopqrstuvwxyz";
      string digits = "0123456789";
      string specialChars = "#?!,-'/`_*$";
      string password = "2woRDsss";
      int score = 0;
      if ((score) >= (minLength))
      {
        score++;
      }
      
      if (Tools.Contains(password, uppercase))
      {
        score++;
      }
      
      if (Tools.Contains(password, lowercase))
      {
        score++;
      }
      
      if (Tools.Contains(password, digits))
      {
        score++;
      }
      
      if (Tools.Contains(password, specialChars))
      {
        score++;
      }
           
      Console.WriteLine(password);
      Console.WriteLine(score);
      
      switch (score) 
      {
        case 5:
        case 4:
          Console.WriteLine("This is an extremely strong password");
          break;
        case 3:
          Console.WriteLine("This is a strong password");
          break;
        case 2:
          Console.WriteLine("This is an average password");
          break;
        case 1:
          Console.WriteLine("This is a weak password");
          break;
        default:
          Console.WriteLine("This password doesn't meet the standards");
          break;
      } 
      Console.ReadLine();
    }
  }
}

You are missing adding 1 point to each password entered due to the line mentioned above. Happy Coding!

Thanks for reminding me to change it.

1 Like

Why is it “if score >= minLength” and not “password.length >= minLength”? That’s what I used my first time through, and it works. The highest score you can get using score >= minLength is 4, how can that statement ever return true? Maybe I’m having a giant noob moment here but that doesn’t make sense to me.

Hello @jbendz. Welcome to the forum.

if (password.Length >= minLength)
{
  score++;
} 

Not sure why you’re asking, but you are correct. It should be as you stated, and I’ve re-illustrated above. There is no need to check if score > minLength.

I have finished this exercise but need some help with regular expressions(Regex); I am not sure how to change a code, for example:

if (!Tools.Contains(password, upperCase))
{
Console.WriteLine(“Password does not contain an uppercase letter, INSECURE!”);
}

into another code that uses Regex to check for password.

Btw, side question is: What is tools.contain? is this “tools” a method that is only built in CodeAcademy program? Or it can be called up using System in visual studio?

Thank you!

If my question is not clear, please write responses tell me so. I will make it more clear.

1 Like

Thanks, and I was asking because in example two posts above me it was shown as score >= minLength. I came to the forum for entirely different reason, but when I saw that it confused me and I thought I might be missing something. Thank you for the response.

Oh. Yeah, that wasn’t an example of what to do. It was code that needed fixing :slightly_smiling_face:

I am using an online compiler and when I try to compile the source code it gives me the following error. A word of notice here, the program runs fine in codecademy panel.

main.cs(18,11): error CS0103: The name `Tools' does not exist in the current context

Source Code

using System;

class MainClass {
  public static void Main (string[] args) {
      int minLength = 8;
      string uppercase = "ABCDEFGHIKLMNOPQRSTVXYZ";
      string lowercase = "abcdefghiklmnopqrstvxyz";
      string digits = "0123456789";
      string specialChars = "!@\"'()*=+,-./";
      Console.Write("Enter a password: ");
      string userInput = Console.ReadLine();
      int passwordLength = userInput.Length;
      int score = 0;
      if (passwordLength >= minLength) {
        score = score + 1;
        //Console.WriteLine($"Score: {score}");
      }
      if (Tools.Contains(userInput, uppercase)) {
        score = score + 1;
        //Console.WriteLine($"Score: {score}");
      }
      if (Tools.Contains(userInput, lowercase)) {
        score = score + 1;
        //Console.WriteLine($"Score: {score}");
      }
      if (Tools.Contains(userInput, digits)) {
        score = score + 1;
        //Console.WriteLine($"Score: {score}");
    }
      if (Tools.Contains(userInput, specialChars)) {
        score = score + 1;
        //Console.WriteLine($"Score: {score}");
      }
        score = score + score;
        Console.WriteLine($"Score: {score}");
  }
}
1 Like

Hello, @madaskalas.

Tools.cs is a separate file included in the exercise. In the Codecademy environment you can see the file by typing ls in the console. If you want to see the code, you can type cat Tools.cs. Tools.Contains is a quite simple method, that you could duplicate into your own code that you are using outside of the CC environment.

Thanks, @midlindner,

Tools.cs should be a separate file, correct? Here is the URL of the project. For some reason, it throws the same error. Can you give me some information about Tools.Contains ? I searched the web, but I only found the String.Contains Method.

1 Like

doesn’t have to be, you could integrate this function into your class, does mean you have to make some small changes.

the codecademy project also has PasswordChecker.csproj file, maybe that is what integrate the two cs files? Some sort of project structure file? Not sure, i barely done any C#

There are a couple of ways you could incorporate the Contains method from Tools.cs
Since you already created the Tools.cs file in your project, you could just wrap your MainClass in the same namespace that the Tools class is in like so:

using System;

namespace PasswordChecker 
{
  class MainClass 
  {
    public static void Main (string[] args) 
    {
     //Main method code
    }
  }
}

Or, you could add the Contains method to your mainClass like so:

class MainClass 
{
  public static void Main (string[] args)
  {
  //Main method code
  //Instead of Tools.Contains(arg1, arg2) you would simply use Contains(arg1, arg2)
  }
  //Add the Contains method here after the Main method, but still inside the MainClass class
  public static bool Contains(string target, string list)
    {
      return target.IndexOfAny(list.ToCharArray()) != -1;
    }
}

Using the second option, you could delete the Tools.cs file from your Repl.

2 Likes

Works great, Thank you!

1 Like

How to set score to zero. when the password is “password” and “1234”
please reply quickly!!!

please see this topic:

How to ask good questions (and get good answers)

your question currently misses a lot context

sounds like you need a conditional (to check username and password are a match, followed by an assignment.