Advertisement
jkonova

Untitled

Aug 31st, 2021
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.27 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.             int countOfChecks = 0;
  13.             bool isCheckForSize = true;
  14.             bool isCheckForLetters = false;
  15.  
  16.             if (password.Length < 6 || password.Length > 10) // проверка за дължината на паролата
  17.             {
  18.                 Console.WriteLine("Password must be between 6 and 10 characters");
  19.                 isCheckForSize = false;
  20.             }
  21.  
  22.             for (int i = 0; i < password.Length; i++)
  23.             {
  24.                 char currentLetter = password[i];
  25.  
  26.                 if (currentLetter >= 48 && currentLetter <= 57) // проверка за цифри в паролата
  27.                 {
  28.                     countOfNumbers++;
  29.                 }
  30.  
  31.                 if ((currentLetter >= 32 && currentLetter <= 47) || (currentLetter >= 58 && currentLetter <= 64) || (currentLetter >= 91 && currentLetter <= 96) || (currentLetter >= 123)) // проверка за други символи
  32.                 {
  33.                     if (countOfChecks == 1)
  34.                     {
  35.                         continue;
  36.                     }
  37.                     Console.WriteLine("Password must consist only of letters and digits");
  38.                     countOfChecks++;
  39.                 }
  40.                 if ((currentLetter >= 65 && currentLetter <= 90) || (currentLetter >= 97 && currentLetter <= 122)) // проверка за букви
  41.                 {
  42.                     isCheckForLetters = true;
  43.                 }
  44.             }
  45.             if (countOfNumbers < 2)
  46.             {
  47.                 Console.WriteLine("Password must have at least 2 digits");
  48.             }
  49.             if (isCheckForLetters == false)
  50.             {
  51.                 if (countOfChecks == 0)
  52.                 {
  53.                     Console.WriteLine("Password must consist only of letters and digits");
  54.                 }
  55.             }
  56.             if (isCheckForSize && countOfChecks == 0 && isCheckForLetters && countOfNumbers >= 2)
  57.             {
  58.                 Console.WriteLine("Password is valid");
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement