Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- struct account
- {
- char user[30];
- char password[30];
- char newpassword[30];
- int control;
- };
- int passwordIsLongEnough(char str[]) //kontrollerar längden på lösenordet
- {
- int i;
- int counter = 0;
- int returnera;
- for (i = 0; str[i] != '\0'; i++)
- counter++;
- if (counter < 8)
- returnera = 0;
- else
- returnera = 1;
- return returnera;
- }
- int passwordContainsDigit(char str[]) //kontrollerar om det finns en siffra i lösenordet
- {
- int i;
- int digit = 0;
- int returnera;
- for (i = 0; str[i] != '\0'; i++)
- if (isdigit(str[i]))
- digit++;
- if (digit > 0)
- returnera = 1;
- else
- returnera = 0;
- return returnera;
- }
- int passwordHasMixedCase(char str[]) //kontrollerar om det finns en stor och en liten bokstav i lösenordet
- {
- int i;
- int returnera = 0;
- int upper = 0;
- int lower = 0;
- for (i = 0; str[i] != '\0'; i++)
- {
- if (islower(str[i]))
- lower++;
- if (isupper(str[i]))
- upper++;
- if (upper > 0 && lower > 0)
- returnera = 1;
- else
- returnera = 0;
- }
- return returnera;
- }
- int isSafePassword(char str[])
- {
- int length;
- int digit;
- int upperlowercase;
- int returnera;
- int faultcounter = 0;
- length = passwordIsLongEnough(str);
- digit = passwordContainsDigit(str);
- upperlowercase = passwordHasMixedCase(str);
- if (length == 1 && digit == 1 && upperlowercase == 1)
- {
- returnera = 1;
- }
- else
- {
- returnera = 0;
- }
- if (length == 0)
- {
- printf("Your password is not long enough\n");
- faultcounter++;
- }
- if (digit == 0)
- {
- printf("Your password must contain at least one number\n");
- faultcounter++;
- }
- if (upperlowercase == 0)
- {
- printf("Your password must contain one upper case letter and one lower case letter\n");
- faultcounter++;
- }
- if (faultcounter > 0)
- {
- printf("you have %d, faults with your password\n", faultcounter);
- }
- return returnera;
- }
- struct account konto(void)
- {
- struct account userAccount;
- printf("Enter username:\n");
- scanf("%s", userAccount.user);
- do
- {
- do
- {
- printf("Enter password:\n");
- scanf("%s", userAccount.password);
- userAccount.control = isSafePassword(userAccount.password);
- } while (userAccount.control == 0);
- printf("Confirm your password:\n");
- scanf("%s", userAccount.newpassword);
- if (strcmp(userAccount.password, userAccount.newpassword) == 0)
- printf("Password confirmed\n");
- else
- printf("Passwords do not match\n");
- } while (strcmp(userAccount.password, userAccount.newpassword) != 0);
- return userAccount;
- }
- int main(void)
- {
- int konton;
- int i;
- printf("How many useraccounts should be used?\n");
- scanf("%d", &konton);
- struct account*arr = (struct account*)malloc(sizeof(struct account)*konton);
- if (arr == NULL)
- printf("Not possible to allocate %d elements", konton);
- else
- for (i = 0; i < konton; i++)
- {
- arr[i] = konto();
- }
- printf("Username: \t \t Password: \n");
- for (i = 0; i < konton; i++)
- printf("%s \t \t %s \n", arr[i].user, arr[i].password);
- free(arr);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement