Strong Password

  • + 0 comments

    MY SOLUTION -- USING REGEX PATTERN IN JAVA

    Pattern num_regex = Pattern.compile("[0-9]");
        Pattern small_alpha_regex = Pattern.compile("[a-z]");
        Pattern cap_alpha_regex = Pattern.compile("[A-Z]");
        Pattern special_regex = Pattern.compile("[-!@#$%^&*()-+]");
    
        int min_len = 6;
        int count = 0;
        if (num_regex.matcher(password).find() && small_alpha_regex.matcher(password).find()
                && cap_alpha_regex.matcher(password).find() && special_regex.matcher(password).find() && n < min_len) {
            while (n != min_len) {
                n++;
                count++;
            }
        }
    
        if (!num_regex.matcher(password).find())
            count++;
        if (!small_alpha_regex.matcher(password).find())
            count++;
        if (!cap_alpha_regex.matcher(password).find())
            count++;
        if (!special_regex.matcher(password).find())
            count++;
    
        n += count;
        if (n >= min_len)
            return count;
        while (n != min_len) {
            n++;
            count++;
        }
        return count;