Introduction
Password validation is a critical step in ensuring the security of user accounts and systems. In this program, we will validate whether a given password meets certain security criteria: the length of the password, the presence of uppercase letters, numbers, and special characters. This process helps prevent weak passwords and enhances system security.
Objective
The objective of this program is to write a C program that validates a password based on the following criteria:
- The password should be at least 8 characters long.
- The password should contain at least one uppercase letter.
- The password should contain at least one number.
- The password should contain at least one special character (e.g., !, @, #, $, etc.).
Code
#include
#include
#include
int validatePassword(char password[]) {
int length = strlen(password);
int hasUpper = 0, hasDigit = 0, hasSpecial = 0;
if(length < 8) {
return 0; // Password too short
}
for(int i = 0; i < length; i++) {
if(isupper(password[i])) {
hasUpper = 1;
} else if(isdigit(password[i])) {
hasDigit = 1;
} else if(ispunct(password[i])) {
hasSpecial = 1;
}
}
if(hasUpper && hasDigit && hasSpecial) {
return 1; // Password is valid
} else {
return 0; // Password does not meet requirements
}
}
int main() {
char password[100];
printf(“Enter your password: “);
scanf(“%s”, password);
if(validatePassword(password)) {
printf(“Password is valid.\n”);
} else {
printf(“Password is invalid. It must be at least 8 characters long, contain at least one uppercase letter, one number, and one special character.\n”);
}
return 0;
}
Explanation of the Program Structure:
- validatePassword function:
- This function takes a string
password[]
as input and checks its length. - It then loops through each character to check if the password contains at least one uppercase letter, one number, and one special character.
- If all criteria are met, the function returns 1 (valid password), otherwise 0 (invalid password).
- This function takes a string
- main function:
- Prompts the user to enter a password.
- It then calls the
validatePassword
function with the entered password and prints whether the password is valid or invalid.
How to Run the Program:
- Save the code in a file named
password_validator.c
. - Open a terminal or command prompt.
- Navigate to the folder where you saved the program file.
- Compile the code using the following command:
- Run the compiled program:
- Enter a password when prompted and the program will tell you whether the password is valid or invalid based on the specified criteria.
Program Explanation
The program is structured as follows:
- validatePassword function: This function takes the password as input and checks whether it meets the specified criteria. It checks the length, the presence of uppercase letters, digits, and special characters using built-in C functions like
strlen
,isupper
,isdigit
, andispunct
. - main function: This function prompts the user to enter a password and then calls
validatePassword
to validate the password. It outputs whether the password is valid or invalid based on the returned result.
How to Run the Program
- Write the code in a text editor and save it as
password_validator.c
. - Open your terminal or command prompt and navigate to the directory where the file is saved.
- Compile the C program using the following command:
gcc password_validator.c -o password_validator
- Run the compiled program with:
./password_validator
- Enter a password when prompted and the program will validate it.