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:

  1. 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).
  2. 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:

  1. Save the code in a file named password_validator.c.
  2. Open a terminal or command prompt.
  3. Navigate to the folder where you saved the program file.
  4. Compile the code using the following command:
    gcc password_validator.c -o password_validator
  5. Run the compiled program:
    ./password_validator
  6. 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, and ispunct.
  • 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

  1. Write the code in a text editor and save it as password_validator.c.
  2. Open your terminal or command prompt and navigate to the directory where the file is saved.
  3. Compile the C program using the following command:
    gcc password_validator.c -o password_validator
  4. Run the compiled program with:
    ./password_validator
  5. Enter a password when prompted and the program will validate it.

 

© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)