Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MethodsExercise
- {
- class Program
- {
- static void Main(string[] args)
- {
- string password = Console.ReadLine();
- bool isValid = CheckPasswordLength(password) &&
- CheckTypeOfCharacter(password) &&
- CheckNumberOfDigits(password);
- if (isValid == true)
- {
- Console.WriteLine("Password is valid");
- }
- else
- {
- if (!CheckPasswordLength(password))
- {
- Console.WriteLine("Password must be between 6 and 10 characters");
- }
- if (!CheckTypeOfCharacter(password))
- {
- Console.WriteLine("Password must consist only of letters and digits");
- }
- if (!CheckNumberOfDigits(password))
- {
- Console.WriteLine("Password must have at least 2 digits");
- }
- }
- }
- private static bool CheckNumberOfDigits(string password)
- {
- int counter = 0;
- foreach (char character in password)
- {
- if (char.IsDigit(character))
- {
- counter++;
- }
- }
- if (counter >= 2)
- {
- return true;
- }
- return false;
- }
- private static bool CheckTypeOfCharacter(string password)
- {
- foreach (char charackter in password)
- {
- if (!char.IsLetterOrDigit(charackter))
- {
- return false;
- }
- }
- return true;
- }
- private static bool CheckPasswordLength(string password)
- {
- if (password.Length >= 6 && password.Length <= 10)
- {
- return true;
- }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment