Program to check Strength of Password

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
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP 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;
}

chevron_right


Output:

Strong

This article is contributed by Apurva Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.