Advertisement
tissiana

PasswordValidator

Oct 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace PasswordValidator
  5. {
  6.     class Program
  7.     {
  8.         static void Main()
  9.         {
  10.             string password = Console.ReadLine();
  11.             int digitsCount = 0;
  12.             int notValidSymbols = 0;
  13.            
  14.  
  15.             for (int i = 0; i < password.Length; i++)
  16.             {
  17.                 bool validChars = ((char)password[i] >= 48 && (char)password[i] <= 57) || ((char)password[i] >= 65 && (char)password[i] <= 90) || ((char)password[i] >= 97 && (char)password[i] <= 122);
  18.  
  19.                 if(!validChars)
  20.                 {
  21.                     notValidSymbols += 1;
  22.                 }
  23.                 if((char) password[i] >= 48 && (char)password [i] <= 57)
  24.                 {
  25.                     digitsCount += 1;
  26.                 }
  27.             }
  28.             if(password.Length < 6 || password.Length > 10)
  29.             {
  30.                 PrintNotEnoughOrTooManyCharacters();
  31.             }
  32.             if(notValidSymbols > 0)
  33.             {
  34.                 PrintNotCorrectSymbols();
  35.             }
  36.  
  37.             if (digitsCount < 2)
  38.             {
  39.                 PrintNotEnoughDigits();
  40.             }
  41.             if(password.Length >= 6 && password.Length <= 10 && notValidSymbols == 0 && digitsCount >= 2)
  42.             {
  43.                 Console.WriteLine("Password is valid");
  44.             }
  45.  
  46.         }
  47.         public static void PrintNotEnoughOrTooManyCharacters()
  48.         {
  49.             Console.WriteLine("Password must be between 6 and 10 characters");
  50.         }
  51.         public static void PrintNotCorrectSymbols()
  52.         {
  53.             Console.WriteLine("Password must consist only of letters and digits");
  54.         }
  55.         public static void PrintNotEnoughDigits()
  56.         {
  57.             Console.WriteLine("Password must have at least 2 digits");
  58.         }
  59.  
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement