Algorithm Assignment
Algorithm Assignment
h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
User users[MAX_USERS];
int userCount = 0;
int validatePassword(const char *password);
void generatePUK(char *puk);
void createAccount();
void login();
void resetPassword();
int findUserIndexByUsername(const char *username);
int validatePassword(const char *password) {
int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;
if (strlen(password) < 8) {
return 0;
}
for (int i = 0; password[i] != '\0'; i++) {
if (isupper(password[i])) hasUpper = 1;
else if (islower(password[i])) hasLower = 1;
else if (isdigit(password[i])) hasDigit = 1;
else if (strchr("@#$!", password[i])) hasSpecial = 1;
}
return hasUpper && hasLower && hasDigit && hasSpecial;
}
void generatePUK(char *puk) {
srand(time(NULL));
for (int i = 0; i < 8; i++) {
puk[i] = 'A' + rand() % 26;
}
puk[8] = '\0';
}
void createAccount() {
char username[50], password[50], confirmPassword[50];
char puk[10];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
printf("Re-enter password: ");
scanf("%s", confirmPassword);
if (strcmp(password, confirmPassword) != 0) {
printf("Passwords do not match.\n");
return;
}
if (!validatePassword(password)) {
printf("Password does not meet the criteria.\n");
return;
}
generatePUK(puk);
strcpy(users[userCount].username, username);
strcpy(users[userCount].password, password);
strcpy(users[userCount].pukCode, puk);
users[userCount].failedAttempts = 0;
users[userCount].isBlocked = 0;
userCount++;
printf("Account created successfully. Your PUK code is: %s\n", puk);
}
void login() {
char username[50], password[50];
int userIndex;
printf("Enter username: ");
scanf("%s", username);
userIndex = findUserIndexByUsername(username);
if (userIndex == -1) {
printf("User not found.\n");
return;
}
if (users[userIndex].isBlocked) {
printf("Account is blocked. Use PUK code to recover.\n");
return;
}
printf("Enter password: ");
scanf("%s", password);
if (strcmp(users[userIndex].password, password) == 0) {
printf("Login successful.\n");
users[userIndex].failedAttempts = 0; // Reset failed attempts
} else {
users[userIndex].failedAttempts++;
printf("Invalid password. Attempts remaining: %d\n",
3 - users[userIndex].failedAttempts);
if (users[userIndex].failedAttempts >= 3) {
users[userIndex].isBlocked = 1;
printf("Account is now blocked.\n");
}
}
}
void resetPassword() {
char username[50], puk[10], newPassword[50];
int userIndex;
printf("Enter username: ");
scanf("%s", username);
userIndex = findUserIndexByUsername(username);
if (userIndex == -1) {
printf("User not found.\n");
return;
}
printf("Enter PUK code: ");
scanf("%s", puk);
if (strcmp(users[userIndex].pukCode, puk) != 0) {
printf("Invalid PUK code.\n");
return;
}
printf("Enter new password: ");
scanf("%s", newPassword);
if (!validatePassword(newPassword)) {
printf("Password does not meet the criteria.\n");
return;
}
strcpy(users[userIndex].password, newPassword);
users[userIndex].failedAttempts = 0;
users[userIndex].isBlocked = 0;
printf("Password reset successful.\n");
}
int findUserIndexByUsername(const char *username) {
for (int i = 0; i < userCount; i++) {
if (strcmp(users[i].username, username) == 0) {
return i;
}
}
return -1;
}
int main() {
int choice;
while (1) {
printf("\n=== Account Management System ===\n");
printf("1. Create Account\n");
printf("2. Login\n");
printf("3. Reset Password\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
createAccount();
break;
case 2:
login();
break;
case 3:
resetPassword();
break;
case 4:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}