alexbancheva

Untitled

Jun 2nd, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3.  
  4. namespace Password_Validator_Methods_Exercises
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string password = Console.ReadLine();
  11.  
  12.             if (LengthPass(password) == false)
  13.             {
  14.                 Console.WriteLine("Password must be between 6 and 10 characters");
  15.             }
  16.  
  17.             if (CheckedLettersAndDigits(password) == false)
  18.             {
  19.                 Console.WriteLine("Password must consist only of letters and digits");
  20.             }
  21.  
  22.             if (CheckedLeastDigits(password) == false)
  23.             {
  24.                 Console.WriteLine("Password must have at least 2 digits");
  25.             }
  26.  
  27.             if (LengthPass(password) == true && CheckedLettersAndDigits(password) == true && CheckedLeastDigits(password) == true)
  28.             {
  29.                 Console.WriteLine("Password is valid");
  30.             }
  31.  
  32.         }
  33.  
  34.         static bool LengthPass(string password)
  35.         {
  36.  
  37.             bool result = false;
  38.  
  39.             if (password.Length >= 6 && password.Length <= 10)
  40.             {
  41.                 result = true;
  42.             }
  43.  
  44.             return result;
  45.         }
  46.  
  47.         static bool CheckedLettersAndDigits(string password)
  48.         {
  49.             bool result = true;
  50.            
  51.  
  52.             for (int i = 0; i < password.Length; i++)
  53.             {
  54.                
  55.                 bool isDigits = true;      // ASCII - 48 - 57;
  56.                 bool isUpperLetters = true;  // ASCII - 65 - 90;
  57.                 bool isLowerLetters = true;  // ASCII  - 97 - 122;
  58.  
  59.                 for (int num = 48; num <= 57; num++)
  60.                 {
  61.                     char currentNum = (char)num;
  62.  
  63.                     if (currentNum == password[i])
  64.                     {
  65.                         isDigits = true;
  66.                         break;
  67.                     }
  68.                     else
  69.                     {
  70.                         isDigits = false;
  71.                     }
  72.                 }
  73.  
  74.                 for (int upperL = 65; upperL <= 90; upperL++)
  75.                 {
  76.                     char upperLet = (char)upperL;
  77.  
  78.                     if (upperL == password[i])
  79.                     {
  80.                         isUpperLetters = true;
  81.                         break;
  82.                     }
  83.                     else
  84.                     {
  85.                         isUpperLetters = false;
  86.                     }
  87.                 }
  88.  
  89.                 for (int lowerL = 97; lowerL <= 122; lowerL++)
  90.                 {
  91.  
  92.                     char lowerLet = (char)lowerL;
  93.  
  94.                     if (lowerLet == password[i])
  95.                     {
  96.                         isLowerLetters = true;
  97.                         break;
  98.                     }
  99.                     else
  100.                     {
  101.                         isLowerLetters = false;
  102.                     }
  103.                 }
  104.  
  105.                 if ((isDigits == false) && (isUpperLetters == false) && (isLowerLetters == false))
  106.                 {
  107.                     result = false;
  108.                     break;
  109.                 }
  110.             }
  111.  
  112.             return result;
  113.         }
  114.  
  115.         static bool CheckedLeastDigits(string password)
  116.         {
  117.             bool result = true;
  118.             int digitsCounter = 0;
  119.  
  120.             for (int i = 0; i < password.Length; i++)
  121.             {
  122.                 for (int digs = 48; digs <= 57; digs++)
  123.                 {
  124.                     char digits = (char)digs;
  125.  
  126.                     if (digits == password[i])
  127.                     {
  128.                         digitsCounter++;
  129.                     }
  130.                 }
  131.             }
  132.  
  133.             if (digitsCounter < 2)
  134.             {
  135.                 result = false;
  136.             }
  137.  
  138.             return result;
  139.         }
  140.        
  141.     }
  142. }
Add Comment
Please, Sign In to add comment