C# Password Check Exercise

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.

psswd1

I meant this, Thank you Very much

 int score=0;
      int len=password.Length;
      
   **if(password=="1234")**
**   {**
**     score=0;**
**   }**
     
       
      else if (len >= 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(score);

I did it like this whats wrong with this?

Why do you think its wrong?

you know only check if password is '1234', you forgot to check if password variable equals 'password' string

For one, you check at the start of the password is equal to 1234 (let’s assume you also checked for password or all excluded words for now) and set the score to 0.
That’s fine, but you should take the score there and not do all the other password checks because they will give a score of at least 1 otherwise (1 for digit in the case of 1234 or 1 for lowercase in the case of password). That may have been why you was trying to do with the first else if?

With regards to checking if the password is equal to an excluded words then the easiest way is probably to have a collection of the excluded words and then check if the password is in that. This allows you to add to it or remove from it easily Vs writing extra if statements for each possible excluded word.

The Tools.Contains() method won’t work in my own independent environment when I copy and paste the code from my ‘Password Checker’ project into my own desktop Visual Studio.

The methods worked fine when I ran it on the website.

Does anyone know if the Tools.Contains(target, list) method was one created from scratch by Codecademy specifically for the ‘Password Check’ exercise?

link to this questions origin: https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-password-checker

        if (Tools.Contains(password, uppercase) == true)
        {
            score++;
        }

        if (Tools.Contains(password, lowercase) == true)
        {
            score++;
        }

        if (Tools.Contains(password, digits) == true)
        {
            score++;
        }

        if (Tools.Contains(password, specialChars) == true)
        {
            score++;
        }

Hey @board3621771149,

First check out @midlindner’s response here: C# Password Check Exercise - #14 by midlindner.

If that doesn’t work for you, then try answering this second question:

What do you mean by “won’t work”?

  1. Is there a specific error that shows up?
  2. Does the program run but the score is wrong?
  3. Something else…?

It seems like a number of people, when they try the project on their own computers, run into a similar issue. This is probably because I didn’t clearly explain Tools.Contains().

Tools.Contains() is a custom tool provided by us. It’s defined in a separate file Tools.cs.

I’ve added some clarification to the project - hopefully it helps future learners avoid this issue!

Screen Shot 2020-06-05 at 8.20.56 PM

2 Likes

Hi

My completed project Password Checker using c#

Cheers