0% found this document useful (0 votes)
7 views3 pages

Algorithm Assignment

The document is a C program for an Account Management System that allows users to create accounts, log in, and reset passwords. It includes user validation for passwords, generates a PUK code for account recovery, and implements account blocking after multiple failed login attempts. The program maintains a list of users and provides a simple command-line interface for user interactions.

Uploaded by

Энх Заяа
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Algorithm Assignment

The document is a C program for an Account Management System that allows users to create accounts, log in, and reset passwords. It includes user validation for passwords, generates a PUK code for account recovery, and implements account blocking after multiple failed login attempts. The program maintains a list of users and provides a simple command-line interface for user interactions.

Uploaded by

Энх Заяа
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>

#define MAX_USERS 100


typedef struct {
char username[50];
char password[50];
char pukCode[10];
int failedAttempts;
int isBlocked;
} User;

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;
}

You might also like