Open In App
Related Articles

Program to check Strength of Password

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

A password is said to be strong if it satisfies the following criteria: 

  1. It contains at least one lowercase English character.
  2. It contains at least one uppercase English character.
  3. It contains at least one special character. The special characters are: !@#$%^&*()-+
  4. Its length is at least 8.
  5. It contains at least one digit.

Given a string, find its strength. Let a strong password is one that satisfies all above conditions. A moderate password is one that satisfies first three conditions and has length at least 6. Otherwise password is week.

Examples : 

Input : “GeeksforGeeks!@12”
Output : Strong

Input : “gfg!@12”
Output : Moderate

Implementation:

C++

// C++ program to check if a given password is
// strong or not.
#include <bits/stdc++.h>
using namespace std;
  
void printStrongNess(string& input)
{
    int n = input.length();
  
    // Checking lower alphabet in string
    bool hasLower = false, hasUpper = false;
    bool hasDigit = false, specialChar = false;
    string normalChars = "abcdefghijklmnopqrstu"
        "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
  
    for (int i = 0; i < n; i++) {
        if (islower(input[i]))
            hasLower = true;
        if (isupper(input[i]))
            hasUpper = true;
        if (isdigit(input[i]))
            hasDigit = true;
  
        size_t special = input.find_first_not_of(normalChars);
        if (special != string::npos)
            specialChar = true;
    }
  
    // Strength of password
    cout << "Strength of password:-";
    if (hasLower && hasUpper && hasDigit && 
        specialChar && (n >= 8))
        cout << "Strong" << endl;
    else if ((hasLower || hasUpper) && 
             specialChar && (n >= 6))
        cout << "Moderate" << endl;
    else
        cout << "Weak" << endl;
}
  
// Driver code
int main()
{
    string input = "GeeksforGeeks!@12";
    printStrongNess(input);
    return 0;
}

                    

Java

// Java implementation for the above approach
import java.io.*;
import java.util.*;
  
class GFG {
    
    
    public static void printStrongNess(String input)
    {
        // Checking lower alphabet in string
        int n = input.length();
        boolean hasLower = false, hasUpper = false,
                hasDigit = false, specialChar = false;
        Set<Character> set = new HashSet<Character>(
            Arrays.asList('!', '@', '#', '$', '%', '^', '&',
                          '*', '(', ')', '-', '+'));
        for (char i : input.toCharArray())
        {
            if (Character.isLowerCase(i))
                hasLower = true;
            if (Character.isUpperCase(i))
                hasUpper = true;
            if (Character.isDigit(i))
                hasDigit = true;
            if (set.contains(i))
                specialChar = true;
        }
        
        // Strength of password
        System.out.print("Strength of password:- ");
        if (hasDigit && hasLower && hasUpper && specialChar
            && (n >= 8))
            System.out.print(" Strong");
        else if ((hasLower || hasUpper || specialChar)
                 && (n >= 6))
            System.out.print(" Moderate");
        else
            System.out.print(" Weak");
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        String input = "GeeksforGeeks!@12";
        printStrongNess(input);
    }
  
      
}
// contributed by Ashish Chhabra

                    

Python3

# Python3 program to check if a given 
# password is strong or not. 
def printStrongNess(input_string):
      
    n = len(input_string) 
  
    # Checking lower alphabet in string 
    hasLower = False
    hasUpper = False
    hasDigit = False
    specialChar = False
    normalChars = "abcdefghijklmnopqrstu"
    "vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
      
    for i in range(n):
        if input_string[i].islower():
            hasLower = True
        if input_string[i].isupper():
            hasUpper = True
        if input_string[i].isdigit():
            hasDigit = True
        if input_string[i] not in normalChars:
            specialChar = True
  
    # Strength of password 
    print("Strength of password:-", end = "")
    if (hasLower and hasUpper and 
        hasDigit and specialChar and n >= 8):
        print("Strong")
          
    elif ((hasLower or hasUpper) and 
          specialChar and n >= 6):
        print("Moderate")
    else:
        print("Weak")
  
# Driver code 
if __name__=="__main__"
      
    input_string = "GeeksforGeeks!@12"
      
    printStrongNess(input_string)
  
# This code is contributed by Yash_R

                    

C#

// C# program for the above approach
  
using System;
using System.Collections.Generic;
using System.Linq;
  
class GFG
{
    public static void PrintStrongNess(string input)
    {
        // Checking lower alphabet in string
        int n = input.Length;
        bool hasLower = false, hasUpper = false,
                hasDigit = false, specialChar = false;
        HashSet<char> set = new HashSet<char>(
            new char[] { '!', '@', '#', '$', '%', '^', '&',
                          '*', '(', ')', '-', '+' });
        foreach (char i in input.ToCharArray())
        {
            if (char.IsLower(i))
                hasLower = true;
            if (char.IsUpper(i))
                hasUpper = true;
            if (char.IsDigit(i))
                hasDigit = true;
            if (set.Contains(i))
                specialChar = true;
        }
        
        // Strength of password
        Console.Write("Strength of password:- ");
        if (hasDigit && hasLower && hasUpper && specialChar
            && (n >= 8))
            Console.Write(" Strong");
        else if ((hasLower || hasUpper || specialChar)
                 && (n >= 6))
            Console.Write(" Moderate");
        else
            Console.Write(" Weak");
    }
  
    // Driver Code
    public static void Main(string[] args)
    {
        string input = "GeeksforGeeks!@12";
        PrintStrongNess(input);
    }
}
// This code is contributed by Prince Kumar

                    

Javascript

// Javascript program for the above approach
  
function printStrongNess(input_string) {
  const n = input_string.length;
  // Checking lower alphabet in string
  let hasLower = false;
  let hasUpper = false;
  let hasDigit = false;
  let specialChar = false;
  const normalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 ";
  
  for (let i = 0; i < n; i++) {
    if (input_string[i] >= "a" && input_string[i] <= "z") {
      hasLower = true;
    }
    if (input_string[i] >= "A" && input_string[i] <= "Z") {
      hasUpper = true;
    }
    if (input_string[i] >= "0" && input_string[i] <= "9") {
      hasDigit = true;
    }
    if (!normalChars.includes(input_string[i])) {
      specialChar = true;
    }
  }
  
  // Strength of password
  let strength = "Weak";
  if (hasLower && hasUpper && hasDigit && specialChar && n >= 8) {
    strength = "Strong";
  } else if ((hasLower || hasUpper) && specialChar && n >= 6) {
    strength = "Moderate";
  }
  
  console.log(`Strength of password: ${strength}`);
}
  
// Driver code
const input_string = "GeeksforGeeks!@12";
printStrongNess(input_string);
  
// This code is contributed by princekumaras

                    

Output
Strength of password:-Strong

Time complexity: O(n)
Auxiliary Space: O(1)

Take control of your online security today by using GeeksforGeeks Random Password Generator, a reliable tool that makes creating strong passwords a breeze.

  



Last Updated : 26 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads