Advertisement
Guest User

Untitled

a guest
Jan 7th, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.97 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define SIZE 50
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include<ctype.h>
  7. #include <time.h>
  8.  
  9. void printAccounts(struct account* userAccounts, int amountOfAccounts);
  10. int enterPassword(struct account* userAccounts, int i);
  11. int isSafePassword(char password[]);
  12. int passwordIsLongEnough(char password[]);
  13. int passwordContainsDigit(char password[]);
  14. int passwordHasMixedCase(char password[]);
  15.  
  16. struct account {
  17.     char name[SIZE];
  18.     char password[SIZE];
  19. };
  20.  
  21.  
  22. int main(void) {
  23.     int amountOfAccounts;
  24.     struct account* userAccounts;
  25.  
  26.     printf("How many accounts?: ");
  27.     scanf("%d", &amountOfAccounts);
  28.  
  29.     userAccounts = (struct account*)malloc(sizeof(struct account)*amountOfAccounts);
  30.     memset(userAccounts, 0, sizeof(struct account)* amountOfAccounts);
  31.  
  32.     int i = 0;
  33.     while (i < amountOfAccounts) {
  34.         if (strlen(userAccounts[i].name) == 0) {
  35.             printf("Enter a username: ");
  36.             scanf("%s", userAccounts[i].name);
  37.         }
  38.         if (enterPassword(userAccounts, i) == 1)
  39.             i++;
  40.     }
  41.  
  42.     printAccounts(userAccounts, amountOfAccounts);
  43.  
  44.     getchar();
  45.     getchar();
  46.     free(userAccounts);
  47.     return 0;
  48. }
  49.  
  50.  
  51. void printAccounts(struct account* userAccounts, int amountOfAccounts) {
  52.     printf("\nHere are all users and passwords:\n");
  53.     for (int i = 0; i < amountOfAccounts; i++) {
  54.         printf("%s\t\t", userAccounts[i].name);
  55.         printf("%s\n", userAccounts[i].password);
  56.     }
  57. }
  58.  
  59.  
  60. int enterPassword(struct account* userAccounts, int i) {
  61.     char passwordconfirm[SIZE];
  62.     printf("Enter a password: ");
  63.     scanf("%s", userAccounts[i].password);
  64.     if (isSafePassword(userAccounts[i].password) == 1) {
  65.         scanf("%s", passwordconfirm);
  66.         if (strcmp(userAccounts[i].password, passwordconfirm) == 0) {
  67.             printf("Password %s is confirmed\n", userAccounts[i].password);
  68.             return 1;
  69.         }
  70.         else
  71.             printf("This password differs from the one you entered. Password not confirmed.\n");
  72.     }
  73. }
  74.  
  75.  
  76. int isSafePassword(char password[]) {
  77.     if (passwordIsLongEnough(password) == 1 && passwordContainsDigit(password) == 1 && passwordHasMixedCase(password) == 1) {
  78.         printf("This is a valid password. Confirm by entering it again: ");
  79.         return 1;
  80.     }
  81.     else {
  82.         if (passwordIsLongEnough(password) != 1)
  83.             printf("The password is too short. Please enter at least 8 characters\n");
  84.         if (passwordContainsDigit(password) != 1)
  85.             printf("The password contains no digits\n");
  86.         if (passwordHasMixedCase(password) != 1)
  87.             printf("The password does not contain mixed case\n");
  88.         return 0;
  89.     }
  90. }
  91.  
  92.  
  93. int passwordIsLongEnough(char password[]) {
  94.     if (strlen(password) >= 8)
  95.         return 1;
  96.     else
  97.         return 0;
  98. }
  99.  
  100.  
  101. int passwordContainsDigit(char password[]) {
  102.     for (int i = 0; password[i] != '\0'; i++)
  103.         if (isdigit(password[i]))
  104.             return 1;
  105.     return 0;
  106. }
  107.  
  108.  
  109. int passwordHasMixedCase(char password[]) {
  110.     for (int i = 0; password[i] != '\0'; i++)
  111.         if (isupper(password[i]))
  112.             for (int i = 0; password[i] != '\0'; i++)
  113.                 if (islower(password[i]))
  114.                     return 1;
  115.     return 0;
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement