我正在尝试构建一个简单的密码验证器:
程序输出:
如果密码少于8个字符并且全部为数字,则输出"非常弱"。
如果密码少于8个字符并且全部为字母,则输出"弱"。
如果密码包含8个或更多字符,并且同时包含数字和字母,则输出"强"。
如果密码包含8个或更多字符,并且同时包含数字、字母和特殊字符,则输出"非常强"。
正如您所见,我知道如何检查字符串是否具有这三种类型之一的字符。但如何检查字符串是否同时包含两种或所有三种类型的字符呢?
#include <iostream>
#include <ctype.h>
#include <cstring>
int main()
{
std::cout << "Enter your new password: ";
std:: string password{};
std::cin >> password;
bool veryweak;
bool weak;
bool strong;
bool verystrong;
if (password.length() < 8)
{
for (int i = 0; i < password.length(); i++)
{
if (isdigit(password[i]))
{
veryweak = true;
}
else if (isalpha(password[i]))
{
weak = true;
}
}
}
else if (password.length() >= 8)
{
for (int i = 0; i < password.length(); i++)
{
//if (password has digits and alphabets)
//strong = true;
//if (password has digits and alphabet and special characters)
//verystrong = true;
}
}
else
{
std::cout << "Password is invalid.";
}
//---------------------------------------------------------------------------------------------------------------------
if (veryweak)
{
std::cout << "Your password is very weak.";
}
else if (weak)
{
std::cout << "Your password is weak.";
}
else if(strong)
{
std::cout << "Your password is strong.";
}
else if (verystrong)
{
std::cout << "Your password is very strong.";
}
return 0;
}