Open In App

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

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

 

Similar Reads