Minimum characters required to make a password strong | Java program to find minimum number of characters required to be added to make a password strong ; Function to count minimum number of characters ; Create the patterns to search digit , upper case alphabet , lower case alphabet and special character ; Search the above patterns in password . ; If no digits are present ; If no upper case alphabet is present ; If no lower case alphabet is present ; If no special character is is present ; Check if the string length after adding the required characters is less than 8 ; Add the number of characters to be added ; Driver Code
import java . util . regex . Pattern ; import java . util . regex . Matcher ; class GFG { static int countCharacters ( String password ) { int count = 0 ; Pattern digit = Pattern . compile ( " ( \\ d ) " ) ; Pattern upper = Pattern . compile ( " ( [ A - Z ] ) " ) ; Pattern lower = Pattern . compile ( " ( [ a - z ] ) " ) ; Pattern spChar = Pattern . compile ( " ( \\ W ) " ) ; Matcher Digit = digit . matcher ( password ) ; Matcher Upper = upper . matcher ( password ) ; Matcher Lower = lower . matcher ( password ) ; Matcher Special = spChar . matcher ( password ) ; if ( ! Digit . find ( ) ) { count ++ ; } if ( ! Upper . find ( ) ) { count ++ ; } if ( ! Lower . find ( ) ) { count ++ ; } if ( ! Special . find ( ) ) { count ++ ; } if ( ( count + password . length ( ) ) < 8 ) { count = count + 8 - ( count + password . length ( ) ) ; } return count ; } public static void main ( String args [ ] ) { String password1 = " Geeksforgeeks " ; System . out . println ( countCharacters ( password1 ) ) ; String password2 = " Geeks1" ; System . out . println ( countCharacters ( password2 ) ) ; } }