Coders Packet

Password Validation using C++

By Srishti Dhaval Patkar

A C++ programme to check the strength of a password. A password must pass some criteria to be a strong password.

This C++ programme checks the strength of the password entered by the user. It asks for a new password until user enters a strong password.

For a password to be strong, it must contain at least 2 digits, 2 of the following special characters ('!', '@', '#', '$', '%', '&', '*') and a length of at least 7 characters.

If a password is weak, it tells the user what changes he has to make in the password. As soon as the user enters a strong password, it saves the password.

Function int valid ( ) checks if the password is valid or not :

int val::valid (char p[],int n)
{
    a=b=c=0;
    for(int i=0;i<n;i++)
    {
        if(isdigit(p[i]))
        {
            a+=1; //counts the number of digits in the entered password
        }
        for (int j=0;j<7;j++)
        {
        
            if(p[i]==x[j])
            {
                b+=1; //counts the number of special characters in the entered password
            }
        }
        c+=1; //counts the number of total characters in the entered password
    }
    if(a>=2&&b>=2&&c>=7)
    {
        cout<<"STRONG\nPassword saved successfully!";
        return 1;
    }
    else
    {
        cout<<"\aWEAK\n";
        if(a<2)
            cout<<"The password must contain at least 2 digits!\n";
        if(b<2)
            cout<<"The password must contain at least 2 of the following special characters ('!', '@', '#', '$', '%', '&', '*')\n";
        if(c<7)
            cout<<"The password must contain at least 7 characters\n";
        return 0;
    }
}

 

Download project

Reviews Report

Submitted by Srishti Dhaval Patkar (srishtipatkar15)

Download packets of source code on Coders Packet