📜  设置强密码所需的最少字符

📅  最后修改于: 2021-05-04 18:21:53             🧑  作者: Mango

给定表示密码的字符串str ,任务是计算为使密码强度高而需要添加的最少字符。
如果密码满足以下条件,则认为密码强度高

  • 它至少包含8个字符。
  • 它包含至少一位数字。
  • 它包含至少一个小写字母。
  • 它包含至少一个大写字母。
  • 它包含至少一个特殊字符,其中包括!@#$%^&*()-+

例子:

方法:可以使用正则表达式解决此问题:

  • 创建正则表达式以检查数字,特殊字符,大写和小写字母。
  • 编译正则表达式,并找到带有密码字符串的匹配项
  • 增加计数器,例如count ,以在任何正则表达式不匹配时添加字符。
  • 添加计数字符密码字符串后,检查字符串的长度小于8或没有。如果是这样,请增加差额以进行计数和打印。

下面是上述方法的实现。

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 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 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


输出:
2
2