Minimum characters required to make a password strong Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: 2 Explanation: By adding one digit and one special character we can make the password strong.Input: str = "Geeks1" Output: 2 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. C++ #include <iostream> #include <regex> // CPP program to find minimum number // of characters required to be added // to make a password strong using namespace std; // Function to count minimum // number of characters int countCharacters(string password) { int count = 0; // Create the patterns to search digit, // upper case alphabet, lower case // alphabet and special character regex digit("(\\d)"); regex upper("([A-Z])"); regex lower("([a-z])"); regex spChar("(\\W)"); // if no digits are present if (!regex_search(password, digit)) { count++; } // if no upper case letters are present if (!regex_search(password, upper)) { count++; } // if no lower case letters are present if (!regex_search(password, lower)) { count++; } // if no special characters are present if (!regex_search(password, spChar)) { count++; } // Check if the string length after adding // the required characters is less than 8 if ((count + password.length()) < 8) { count = count + 8 - (count + password.length()); } return count; } int main() { string password1 = "Geeksforgeeks"; cout << countCharacters(password1) << endl; string password2 = "Geeks1"; cout << countCharacters(password2) << endl; return 0; } 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 JavaScript // JavaScript function to find minimum number // of characters required to be added // to make a password strong function countCharacters(password) { let count = 0; // Create the patterns to search digit, // upper case alphabet, lower case // alphabet and special character let digit = /(\d)/; let upper = /([A-Z])/; let lower = /([a-z])/; let spChar = /(\W)/; // Search the above patterns in password. let Digit = digit.exec(password); let Upper = upper.exec(password); let Lower = lower.exec(password); let Special = spChar.exec(password); // If no digits are present if (!Digit) { count++; } // If no upper case alphabet is present if (!Upper) { count++; } // If no lower case alphabet is present if (!Lower) { count++; } // If no special character is present if (!Special) { 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 let password1 = "Geeksforgeeks"; console.log(countCharacters(password1)); let password2 = "Geeks1"; console.log(countCharacters(password2)); // This code is contributed by anskalyan3 C# using System; using System.Text.RegularExpressions; class Program { // 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 Regex digit = new Regex("(\\d)"); Regex upper = new Regex("([A-Z])"); Regex lower = new Regex("([a-z])"); Regex spChar = new Regex("(\\W)"); // if no digits are present if (!digit.IsMatch(password)) { count++; } // if no upper case letters are present if (!upper.IsMatch(password)) { count++; } // if no lower case letters are present if (!lower.IsMatch(password)) { count++; } // if no special characters are present if (!spChar.IsMatch(password)) { count++; } // Check if the string length after adding // the required characters is less than 8 if ((count + password.Length) < 8) { count = count + 8 - (count + password.Length); } return count; } static void Main(string[] args) { string password1 = "Geeksforgeeks"; Console.WriteLine(CountCharacters(password1)); string password2 = "Geeks1"; Console.WriteLine(CountCharacters(password2)); Console.ReadLine(); } } // This code is contributed by Prince Kumar Output: 2 2 Comment More infoAdvertise with us Next Article How to validate a Password using Regular Expressions in Java P prashant_srivastava Follow Improve Article Tags : Strings Pattern Searching DSA java-regular-expression CPP-regex +1 More Practice Tags : Pattern SearchingStrings Similar Reads Program to check Strength of Password A password is said to be strong if it satisfies the following criteria: It contains at least one lowercase English character.It contains at least one uppercase English character.It contains at least one special character. The special characters are: !@#$%^&*()-+Its length is at least 8.It contai 5 min read Strong Password Suggester Program Given a password entered by the user, check its strength and suggest some password if it is not strong. Criteria for strong password is as follows : A password is strong if it has : At least 8 characters At least one special char At least one number At least one upper and one lower case char. Exampl 15 min read Program to check the validity of password without using regex. Password checker program basically checks if a password is valid or not based on the password policies mention below: Password should not contain any space. Password should contain at least one digit(0-9). Password length should be between 8 to 15 characters. Password should contain at least one low 9 min read Generate Passwords of given length Given an integer N, the task is to generate random passwords of length N of easy, medium and strong level each. Easy level Password: Consists of only numbers or letters. Medium level Password: Consists of Letters as well as numbers. Strong level Password - Consists of Letters, numbers, and/or specia 11 min read How to validate a Password using Regular Expressions in Java Given a password, the task is to validate the password with the help of Regular Expression. A password is considered valid if all the following constraints are satisfied: It contains at least 8 characters and at most 20 characters.It contains at least one digit.It contains at least one upper case al 3 min read What is the Minimum Length of Password in Django? In Django, the default minimum period for passwords is 8 characters. This method that while you create a personal account or alternate a password, Django enforces a minimal password period of eight characters by default. However, you could customize this minimum length by enhancing your mission's se 3 min read Like