我正在尝试制作一个密码验证程序,用于测试以下条件:
The password should be at least 8 characters long
The password should contain at least one uppercase and at least one lowercase
The password should have at least one digit
The password should 1 special character: ! @ # $ % & * : ;
我让程序在没有函数的情况下工作,所以我尝试创建一个测试每个条件的函数,但由于某种原因,strlen在我的函数中不起作用。我包括cstring和cctype但我无法弄清楚为什么错误仍然存在。请帮忙!
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
void criteraValidation(string str1, int up, int low, int digit, int special);
int main()
{
const int SIZE = 20;
char password[SIZE];
int Upper, Lower, Digit, SpecialChar;
cout << "Password Validation Criteria" << endl;
cout << "The password should be at least 8 characters long" << endl;
cout << "The password should contain at least one uppercase and at least one lowercase letter" << endl;
cout << "The password should have at least one digit" << endl;
cout << "The password should 1 special character: ! @ # $ % & * : ; " << endl;
do
{
Upper = Lower = Digit = SpecialChar = 0;
cout << "Enter password: ";
cin.getline(password, SIZE);
}
while (SpecialChar == 0 || Upper == 0 || Lower == 0 || Digit == 0 || strlen(password) < 8);
cout << "All crieria have been met! Password: " << password << endl;
return 0;
}
void criteraValidation(string str1, int up, int low, int digit, int special)
{
for (int i = 0; i < strlen(str1); i++)
{
if (isupper(str1[i])) // checks uppercase letters
up++;
if (islower(str1[i])) // checks lowercase letters
low++;
if(isdigit(str1[i])) // checks digits
digit++;
if (ispunct(str1[i])) // checks special characters
special++;
}
if (strlen(str1) < 8)
cout << "Must be at least 8 characters long.\n";
if (up == 0)
cout << "Must be at least 1 uppercase letter.\n";
if (low == 0)
cout << "Must be at least 1 lowercase letter.\n";
if (digit == 0)
cout << "Must have at least 1 digit.\n";
if (special == 0)
cout << "Must have 1 special character" << endl;
}
答案 0 :(得分:1)
从评论中,您可能想要这样的内容:
void criteraValidation(const string& str1, // Pass by const reference since it won't be modified.
int& up, int& low, int& digit, int& special);
void criteraValidation(const string& str1,
int& up, int& low, int& digit, int& special)
{
up = 0;
low = 0;
digit = 0;
special = 0;
const std::string::size_type length = str1.length();
for (int i = 0; i < length; i++)
{
if (isupper(str1[i])) // checks uppercase letters
up++;
if (islower(str1[i])) // checks lowercase letters
low++;
if(isdigit(str1[i])) // checks digits
digit++;
if (ispunct(str1[i])) // checks special characters
special++;
}
if (str1.length() < 8)
cout << "Must be at least 8 characters long.\n";
if (up == 0)
cout << "Must be at least 1 uppercase letter.\n";
if (low == 0)
cout << "Must be at least 1 lowercase letter.\n";
if (digit == 0)
cout << "Must have at least 1 digit.\n";
if (special == 0)
cout << "Must have 1 special character" << endl;
}
主要:
do
{
Upper = Lower = Digit = SpecialChar = 0;
cout << "Enter password: ";
cin.getline(password, SIZE);
criteraValidation(password, Upper, Lower, Digit, SpecialChar);
}
while (SpecialChar == 0
|| Upper == 0
|| Lower == 0
|| Digit == 0
|| (password.length() < 8));
注意:isupper
,islower
,isdigit
和ispunct
是独占的(字符不能同时为大写和小写),所以你可能想要使用if-else-if
阶梯,以便不是所有的比较都会被执行:
const char c = str1[i];
if (isupper(c))
{
++up;
}
else if (islower(c))
{
++low;
}
else if (isdigit(c))
{
++digit;
}
else if (ispunct(c))
{
++special;
}
else
{
// Put code here for characters not of above types.
}
例如,对于字符“A”,只执行一个函数,而所有比较都在代码中执行。
答案 1 :(得分:-1)
您的示例显示strlen()处理char数组。当您将其传递给函数时,您需要使用&#39;字符串&#39;进行发送。那是一个std :: string吗?你需要调用strlen(str1.c_str())吗?