Program to check Strength of Password - GeeksforGeeks

28 Feb, 2022
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 contains at least one digit.
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 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
C++
Java
Python3
// 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 codeint main(){    string input = "GeeksforGeeks!@12";    printStrongNess(input);    return 0;}
// Java implementation for the above approachimport 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 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 codeif __name__=="__main__":         input_string = "GeeksforGeeks!@12"         printStrongNess(input_string) # This code is contributed by Yash_R
Strength of password:-Strong
This article is contributed by Apurva Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Yash_R
ashishchhabra2
avtarkumar719
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Check for Balanced Brackets in an expression (well-formedness) using Stack
Python program to check if a string is palindrome or not
KMP Algorithm for Pattern Searching
Different methods to reverse a string in C/C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Check whether two strings are anagram of each other
Top 50 String Coding Problems for Interviews