Strong passwords are at least 8 characters in length, contain both upper- and lo
ID: 3788667 • Letter: S
Question
Strong passwords are at least 8 characters in length, contain both upper- and lower-case letters, as well as some digits and special characters. Use the requirements (specified below) for a password to define in BNF a grammar for <strong password>
A valid password must contain the following elements:
- Standard U.S. keyboard characters only (such as the letters A through Z, the numbers 0 through 9, or the special characters on the top row of the keyboard)
- A minimum of 8 characters
- A maximum of 32 characters
- At least 1 number
- At least 1 of these special characters: ! $ & * < >
Explanation / Answer
package com.chegg.strongpassword;
import java.util.Scanner;
public class StrongPasswordChecker {
public static boolean isSpecialCharacter(Character c)
{
return c.toString().matches("[^a-z A-Z0-9]");
}
/*
* This method is used to valid password given by user whether method
* contains character,letter(lower and upper), numeric value and special character if contains return true or
* false
*
* @param password
*
* @return boolean
*/
public static boolean isValid(String password) {
int length = password.length();
boolean lower = false;
boolean upper = false;
boolean number = false;
boolean special = false;
// check condition if character length 6-10
if ((length >= 8) && (length <= 32)) {
{
for (int i = 0; i < length; i++) {
// check condition if password contains digit or not
if (Character.isDigit(password.charAt(i))) {
number = true;
}
// check condition if password contains lower case letter or not
if (Character.isLowerCase(password.charAt(i))) {
lower = true;
}
// check condition if password contains upper case letter or not
if (Character.isUpperCase(password.charAt(i))) {
upper = true;
}
// check condition if password contains special or not
if(StrongPasswordChecker.isSpecialCharacter(password.charAt(i))){
special = true;
}
}
}
} else {
System.out.println("condition failed!");
}
return (number && lower && upper && special);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter a given password : ");
String password = scan.nextLine();
System.out.println(isValid(password));
if (isValid(password)) {
System.out.println("Welcome password is correct");
} else {
System.out.println("Invalid Passowrd. Password, " + "must contain atleast 1 lower case, "
+ "must contain atleast 1 upper case, " + "must contain atleast 1 special character, "
+ " 1 digit, 1" + " and " + "characters between 8-32 long");
}
scan.close();
}
}