Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- namespace Password_Validator_Methods_Exercises
- {
- class Program
- {
- static void Main(string[] args)
- {
- string password = Console.ReadLine();
- if (LengthPass(password) == false)
- {
- Console.WriteLine("Password must be between 6 and 10 characters");
- }
- if (CheckedLettersAndDigits(password) == false)
- {
- Console.WriteLine("Password must consist only of letters and digits");
- }
- if (CheckedLeastDigits(password) == false)
- {
- Console.WriteLine("Password must have at least 2 digits");
- }
- if (LengthPass(password) == true && CheckedLettersAndDigits(password) == true && CheckedLeastDigits(password) == true)
- {
- Console.WriteLine("Password is valid");
- }
- }
- static bool LengthPass(string password)
- {
- bool result = false;
- if (password.Length >= 6 && password.Length <= 10)
- {
- result = true;
- }
- return result;
- }
- static bool CheckedLettersAndDigits(string password)
- {
- bool result = true;
- for (int i = 0; i < password.Length; i++)
- {
- bool isDigits = true; // ASCII - 48 - 57;
- bool isUpperLetters = true; // ASCII - 65 - 90;
- bool isLowerLetters = true; // ASCII - 97 - 122;
- for (int num = 48; num <= 57; num++)
- {
- char currentNum = (char)num;
- if (currentNum == password[i])
- {
- isDigits = true;
- break;
- }
- else
- {
- isDigits = false;
- }
- }
- for (int upperL = 65; upperL <= 90; upperL++)
- {
- char upperLet = (char)upperL;
- if (upperL == password[i])
- {
- isUpperLetters = true;
- break;
- }
- else
- {
- isUpperLetters = false;
- }
- }
- for (int lowerL = 97; lowerL <= 122; lowerL++)
- {
- char lowerLet = (char)lowerL;
- if (lowerLet == password[i])
- {
- isLowerLetters = true;
- break;
- }
- else
- {
- isLowerLetters = false;
- }
- }
- if ((isDigits == false) && (isUpperLetters == false) && (isLowerLetters == false))
- {
- result = false;
- break;
- }
- }
- return result;
- }
- static bool CheckedLeastDigits(string password)
- {
- bool result = true;
- int digitsCounter = 0;
- for (int i = 0; i < password.Length; i++)
- {
- for (int digs = 48; digs <= 57; digs++)
- {
- char digits = (char)digs;
- if (digits == password[i])
- {
- digitsCounter++;
- }
- }
- }
- if (digitsCounter < 2)
- {
- result = false;
- }
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment