Skip to content

Instantly share code, notes, and snippets.

@AntonGrig
Created July 26, 2019 13:47
Show Gist options
  • Save AntonGrig/dbeaa3fb610b0ee86dd50b863315f034 to your computer and use it in GitHub Desktop.
Save AntonGrig/dbeaa3fb610b0ee86dd50b863315f034 to your computer and use it in GitHub Desktop.
PwCS
using System;
using System.Linq;
namespace Password_security_checker
{
class Program
{
public static void Main(string[] args)
{
Start:
int score = 0;
Console.WriteLine("Enter your password:");
string password = Console.ReadLine();
int length = password.Length;
Console.WriteLine("Repeat your password:");
string repeatPassword = Console.ReadLine();
if (length >= 8)
{
score++;
}
else
{
Console.WriteLine("Password is too short");
goto Start;
}
if (password.Any(char.IsUpper))
{
score++;
}
if (password.Any(char.IsLower))
{
score++;
}
if (password.Any(char.IsDigit))
{
score++;
}
if (password.Any(char.IsSymbol))
{
score++;
}
if (password == repeatPassword)
{
Console.WriteLine("Your password is: " + password);
}
else if (password != repeatPassword)
{
Console.WriteLine("Password does not match");
goto Start;
}
Console.WriteLine("Your password score is " + score);
}
}
}
@jackinf
Copy link

jackinf commented Jul 26, 2019

Clean coding tips:

#1: Remove empty spaces https://gist.github.com/AntonGrig/dbeaa3fb610b0ee86dd50b863315f034#file-code-cs-L5-L6
The correct way of formatting the code is putting a statement, and then the curly brace goes on the next line without any whitespaces.

Also, the lines of code should not be separated by more than 2 empty lines.

#2: using "goto"s is considered an anti-pattern https://gist.github.com/AntonGrig/dbeaa3fb610b0ee86dd50b863315f034#file-code-cs-L12. If you're using it then you need to rethink the strategy.

I suggest using while or do-while loop (tutorial: https://www.tutorialsteacher.com/csharp/csharp-do-while-loop)

e.g.

while (true) {
   // check the password, store the result in a variable, like isValidPassword

  // then...
  if (isValidPassword) {
    break; // exit the loop
  } else {
    Console.WriteLine("Password does not match or is too short");
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment