Advertisement
raziel13

methods_ex_04_password_validator

Dec 8th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _04_pass_validator
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)//100/100
  12.         {
  13.             string password = Console.ReadLine();//input word
  14.  
  15.             if (CheckIfIsLongEnough(password)  + CheckIfItsOnlyLettersAndSymbols(password) + CheckIfItsAtLeastTwoDigits(password) == 3)
  16.             {
  17.                 Console.WriteLine("Password is valid");
  18.             }
  19.  
  20.  
  21.         }
  22.  
  23.         static int CheckIfIsLongEnough(string password)//check for lenght of input
  24.         {
  25.             int control = 1;
  26.             int counter = 0;
  27.  
  28.             for (int i = 0; i < password.Length; i++)
  29.             {
  30.                 counter++;
  31.             }
  32.             if (counter < 6 || counter > 10)//not long enough
  33.             {
  34.                 Console.WriteLine("Password must be between 6 and 10 characters");
  35.                 return control = 0;
  36.             }
  37.             else
  38.             {
  39.                 return control;//long enough, return 1
  40.             }
  41.         }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.         static int CheckIfItsOnlyLettersAndSymbols(string password)//check only letters and symbols
  49.         {
  50.             int control = 1;
  51.  
  52.             foreach (var symbol in password)
  53.             {
  54.                 if (symbol < 48 || (symbol > 57 && symbol < 65) || (symbol > 90 && symbol < 97) || symbol > 122)//check ascii table
  55.                 {
  56.                     Console.WriteLine("Password must consist only of letters and digits");
  57.                     return control = 0;
  58.                 }
  59.             }
  60.             return control;//return 1
  61.         }
  62.         static int CheckIfItsAtLeastTwoDigits(string password)
  63.         {
  64.             int counter = 0;
  65.             int control = 1;
  66.             for (int i = 0; i < password.Length; i++)
  67.             {
  68.                 if (password[i] == 48 || password[i] == 49 || password[i] == 50 || password[i] == 51 || password[i] == 52 || password[i] == 53 || password[i] == 54 || password[i] == 55 || password[i] == 56 || password[i] == 57)
  69.                 {
  70.                     counter++;
  71.                 }
  72.             }
  73.             if (counter < 2)
  74.             {
  75.                 Console.WriteLine("Password must have at least 2 digits");
  76.                 return control = 0;
  77.             }
  78.             else
  79.             {
  80.                 return control;
  81.             }
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement