Advertisement
Iskrenov84

04. Password Validator

Feb 10th, 2022
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace P4._PasswordValidator
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             char[] password = Console.ReadLine().ToCharArray();
  11.  
  12.             if (password.Length < 6 || password.Length > 10)
  13.             {
  14.                 Console.WriteLine("Password must be between 6 and 10 characters");
  15.             }
  16.             int digitsInPassword = 0;
  17.             int noLetterOrNumber = 0;
  18.             for (int i = 0; i < password.Length; i++)
  19.             {
  20.                 if (char.IsDigit(password[i]))
  21.                 {
  22.                     digitsInPassword++;
  23.                 }
  24.                 if (!char.IsLetter(password[i]) && !char.IsDigit(password[i]))
  25.                 {
  26.                     noLetterOrNumber++;
  27.                 }
  28.             }
  29.             if (noLetterOrNumber > 0)
  30.             {
  31.                 Console.WriteLine("Password must consist only of letters and digits");
  32.             }
  33.             if (digitsInPassword < 2)
  34.             {
  35.                 Console.WriteLine("Password must have at least 2 digits");
  36.             }
  37.             if (digitsInPassword >= 2 && password.Length >= 6 && password.Length <= 10 && noLetterOrNumber == 0)
  38.             {
  39.                 Console.WriteLine("Password is valid");
  40.             }
  41.         }
  42.     }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement