 Open in App
Not now

# Minimum characters required to make a password strong

• Difficulty Level : Medium
• Last Updated : 24 Nov, 2022

Given string str denoting a password, the task is to count the minimum characters that need to be added to make the password strong
A password is said to be strong if it satisfies the following criteria:

• It contains at least 8 characters.
• It contains at least one digit.
• It contains at least one lower case alphabet.
• It contains at least one upper case alphabet.
• It contains at least one special character which includes !@#\$%^&*()-+

Examples:

Input: str = “Geeksforgeeks”
Output:
Explanation:
By adding one digit and one special character we can make the password strong.
Input: str = “Geeks1”
Output:
Explanation:
A special character along with one more character needs to be added to make the password strong.

Approach: This problem can be solved using Regular Expressions

• Create the regular expressions to check digit, special character, upper case, and lower case alphabet.
• Compile the regular expressions and find matches with the password string.
• Increase the counter, say, count, to add characters whenever any of the regular expression does not match.
• After adding count characters to the password string, check if the length of the string is less than 8 or not. If so, add the difference to count and print.

Below is the implementation of the above approach.

## Java

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

## Python3

 `# Python3 program to find``# minimum number of characters``# required to be added to make``# a password strong``import` `re` `# Function to count minimum``# number of characters``def` `countCharacters(password):` `    ``count ``=` `0` `    ``# Create the patterns to search digit,``    ``# upper case alphabet, lower case``    ``# alphabet and special character``    ``digit ``=` `re.``compile``(``"(\\d)"``)``    ``upper ``=` `re.``compile``(``"([A-Z])"``)``    ``lower ``=` `re.``compile``(``"([a-z])"``)``    ``spChar ``=` `re.``compile``(``"(\\W)"``)` `    ``# Search the above patterns``    ``# in password.` `    ``# If no digits are present``    ``if``(``not` `re.search(digit,``                     ``password)):``        ``count ``+``=` `1` `    ``# If no upper case alphabet``    ``# is present``    ``if``(``not` `re.search(upper,``                     ``password)):``        ``count ``+``=` `1` `    ``# If no lower case alphabet``    ``# is present``    ``if``(``not` `re.search(lower,``                     ``password)):``        ``count ``+``=` `1` `    ``# If no special character``    ``# is present``    ``if``(``not` `re.search(spChar,``                     ``password)):``        ``count ``+``=` `1` `    ``# Check if the string length``    ``# after adding the required``    ``# characters is less than 8``    ``if``((count ``+``        ``len``(password)) < ``8``):` `        ``# Add the number of``        ``# characters to be added``        ``count ``=` `count ``+` `8` `-` `(count ``+``                             ``len``(password))` `    ``return` `count` `# Driver code``password1 ``=` `"Geeksforgeeks"``print``(countCharacters(password1))` `password2 ``=` `"Geeks1"``print``(countCharacters(password2))` `# This code is contributed by avanitrachhadiya2155`

Output:

```2
2```

My Personal Notes arrow_drop_up