Introduction
In the age of digital security, ensuring that users choose strong and secure passwords is of utmost importance.
A password is considered strong if it meets certain criteria that make it difficult for unauthorized individuals
to guess or crack. These criteria typically include having a mix of uppercase letters, numbers, and special characters,
as well as a minimum length requirement.
This program will validate whether a given password meets these criteria by checking the length,
the presence of uppercase letters, numbers, and special characters. If the password satisfies all the conditions,
it will be considered valid; otherwise, the user will be prompted with appropriate error messages.
Objective
The objective of this C++ program is to validate a password based on the following criteria:
- Minimum length of 8 characters
- At least one uppercase letter (A-Z)
- At least one number (0-9)
- At least one special character (e.g., @, #, $, etc.)
Code Implementation
#include
#include
#include
#include
using namespace std;
// Function to validate the password
bool validatePassword(const string& password) {
if (password.length() < 8) {
cout << "Password must be at least 8 characters long." << endl;
return false;
}
bool hasUpperCase = false, hasDigit = false, hasSpecialChar = false;
// Loop through the password to check conditions
for (char c : password) {
if (isupper(c)) hasUpperCase = true;
if (isdigit(c)) hasDigit = true;
if (ispunct(c)) hasSpecialChar = true;
}
// Check if all conditions are met
if (!hasUpperCase) {
cout << "Password must contain at least one uppercase letter." << endl;
return false;
}
if (!hasDigit) {
cout << "Password must contain at least one number." << endl;
return false;
}
if (!hasSpecialChar) {
cout << "Password must contain at least one special character." << endl;
return false;
}
// If all checks pass, the password is valid
return true;
}
int main() {
string password;
cout << "Enter a password to validate: "; cin >> password;
if (validatePassword(password)) {
cout << "Password is valid!" << endl;
} else {
cout << "Password is invalid. Please try again." << endl;
}
return 0;
}
Explanation of the Program Structure
This C++ program starts by including the necessary libraries:
- <iostream> – For input and output operations (e.g., cin, cout).
- <cctype> – For character handling functions like isupper(), isdigit(), and ispunct().
- <string> – To use the string class to store and manipulate the password.
- <regex> – Not actually used here, but could be used for more complex validation in future improvements.
The main function starts by prompting the user to input a password. The inputted password is then passed to the
validatePassword()
function, which checks the following:
- Password Length: The password must have a minimum of 8 characters.
- Uppercase Letter: The password must contain at least one uppercase letter.
- Number: The password must contain at least one digit (0-9).
- Special Character: The password must contain at least one special character (such as @, #, $, etc.).
If the password satisfies all the conditions, the function returns true
, and the program prints “Password is valid!”.
If any condition is not met, the program prints an error message explaining what is wrong with the password.
How to Run the Program
Follow these steps to run the program:
- Step 1: Open your C++ development environment (e.g., Code::Blocks, Visual Studio, or any IDE that supports C++).
- Step 2: Create a new C++ project or file.
- Step 3: Copy and paste the provided code into your C++ source file.
- Step 4: Compile the program to ensure there are no syntax errors.
- Step 5: Run the program, input a password when prompted, and the program will validate it based on the criteria.