Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #define SIZE 50
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include<ctype.h>
- #include <time.h>
- void printAccounts(struct account* userAccounts, int amountOfAccounts);
- int enterPassword(struct account* userAccounts, int i);
- int isSafePassword(char password[]);
- int passwordIsLongEnough(char password[]);
- int passwordContainsDigit(char password[]);
- int passwordHasMixedCase(char password[]);
- struct account {
- char name[SIZE];
- char password[SIZE];
- };
- int main(void) {
- int amountOfAccounts;
- struct account* userAccounts;
- printf("How many accounts?: ");
- scanf("%d", &amountOfAccounts);
- userAccounts = (struct account*)malloc(sizeof(struct account)*amountOfAccounts);
- memset(userAccounts, 0, sizeof(struct account)* amountOfAccounts);
- int i = 0;
- while (i < amountOfAccounts) {
- if (strlen(userAccounts[i].name) == 0) {
- printf("Enter a username: ");
- scanf("%s", userAccounts[i].name);
- }
- if (enterPassword(userAccounts, i) == 1)
- i++;
- }
- printAccounts(userAccounts, amountOfAccounts);
- getchar();
- getchar();
- free(userAccounts);
- return 0;
- }
- void printAccounts(struct account* userAccounts, int amountOfAccounts) {
- printf("\nHere are all users and passwords:\n");
- for (int i = 0; i < amountOfAccounts; i++) {
- printf("%s\t\t", userAccounts[i].name);
- printf("%s\n", userAccounts[i].password);
- }
- }
- int enterPassword(struct account* userAccounts, int i) {
- char passwordconfirm[SIZE];
- printf("Enter a password: ");
- scanf("%s", userAccounts[i].password);
- if (isSafePassword(userAccounts[i].password) == 1) {
- scanf("%s", passwordconfirm);
- if (strcmp(userAccounts[i].password, passwordconfirm) == 0) {
- printf("Password %s is confirmed\n", userAccounts[i].password);
- return 1;
- }
- else
- printf("This password differs from the one you entered. Password not confirmed.\n");
- }
- }
- int isSafePassword(char password[]) {
- if (passwordIsLongEnough(password) == 1 && passwordContainsDigit(password) == 1 && passwordHasMixedCase(password) == 1) {
- printf("This is a valid password. Confirm by entering it again: ");
- return 1;
- }
- else {
- if (passwordIsLongEnough(password) != 1)
- printf("The password is too short. Please enter at least 8 characters\n");
- if (passwordContainsDigit(password) != 1)
- printf("The password contains no digits\n");
- if (passwordHasMixedCase(password) != 1)
- printf("The password does not contain mixed case\n");
- return 0;
- }
- }
- int passwordIsLongEnough(char password[]) {
- if (strlen(password) >= 8)
- return 1;
- else
- return 0;
- }
- int passwordContainsDigit(char password[]) {
- for (int i = 0; password[i] != '\0'; i++)
- if (isdigit(password[i]))
- return 1;
- return 0;
- }
- int passwordHasMixedCase(char password[]) {
- for (int i = 0; password[i] != '\0'; i++)
- if (isupper(password[i]))
- for (int i = 0; password[i] != '\0'; i++)
- if (islower(password[i]))
- return 1;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement