svephoto

Password Validator [C#]

Sep 1st, 2021 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PasswordValidator
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             string password = Console.ReadLine();
  10.  
  11.             int countOfNumbers = 0;
  12.  
  13.             bool isDigitOrLetter = true;
  14.             bool isDigit = false;
  15.             bool isLetter = false;
  16.  
  17.             for (int i = 0; i < password.Length; i++)
  18.             {
  19.                 char currentSymbol = password[i];
  20.  
  21.                 isDigit = Char.IsDigit(currentSymbol); // използване на готов метод за проверка на цифри
  22.                 isLetter = Char.IsLetter(currentSymbol); // използване на готов метод за проверка на букви
  23.  
  24.                 if (isDigit) // ако някой от символите е цифра - броим
  25.                 {
  26.                     countOfNumbers++;
  27.                 }
  28.  
  29.                 if (!isDigit && !isLetter) // ако не е нито цифра, нито буква, правим каквото трябва
  30.                 {
  31.                     isDigitOrLetter = false;
  32.                 }
  33.             }
  34.  
  35.             if (password.Length < 6 || password.Length > 10) // проверка за дължината на паролата
  36.             {
  37.                 Console.WriteLine("Password must be between 6 and 10 characters");
  38.             }
  39.  
  40.             if (!isDigitOrLetter)
  41.             {
  42.                 Console.WriteLine("Password must consist only of letters and digits");
  43.             }
  44.  
  45.             if (countOfNumbers <= 1)
  46.             {
  47.                 Console.WriteLine("Password must have at least 2 digits");
  48.             }
  49.  
  50.             if (isDigitOrLetter && countOfNumbers > 1)
  51.             {
  52.                 Console.WriteLine("Password is valid");
  53.             }
  54.         }
  55.     }
  56. }
  57.  
Add Comment
Please, Sign In to add comment