Advertisement
Guest User

Untitled

a guest
Jun 11th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. struct account
  7. {
  8. char user[30];
  9. char password[30];
  10. char newpassword[30];
  11. int control;
  12. };
  13.  
  14. int passwordIsLongEnough(char str[]) //kontrollerar längden på lösenordet
  15. {
  16. int i;
  17. int counter = 0;
  18. int returnera;
  19. for (i = 0; str[i] != '\0'; i++)
  20. counter++;
  21. if (counter < 8)
  22. returnera = 0;
  23.  
  24. else
  25. returnera = 1;
  26. return returnera;
  27. }
  28.  
  29. int passwordContainsDigit(char str[]) //kontrollerar om det finns en siffra i lösenordet
  30. {
  31. int i;
  32. int digit = 0;
  33. int returnera;
  34. for (i = 0; str[i] != '\0'; i++)
  35.  
  36. if (isdigit(str[i]))
  37. digit++;
  38. if (digit > 0)
  39. returnera = 1;
  40. else
  41. returnera = 0;
  42. return returnera;
  43. }
  44.  
  45. int passwordHasMixedCase(char str[]) //kontrollerar om det finns en stor och en liten bokstav i lösenordet
  46. {
  47. int i;
  48. int returnera = 0;
  49. int upper = 0;
  50. int lower = 0;
  51.  
  52. for (i = 0; str[i] != '\0'; i++)
  53. {
  54. if (islower(str[i]))
  55. lower++;
  56.  
  57. if (isupper(str[i]))
  58. upper++;
  59.  
  60. if (upper > 0 && lower > 0)
  61. returnera = 1;
  62.  
  63. else
  64. returnera = 0;
  65. }
  66.  
  67. return returnera;
  68. }
  69.  
  70. int isSafePassword(char str[])
  71. {
  72. int length;
  73. int digit;
  74. int upperlowercase;
  75. int returnera;
  76. int faultcounter = 0;
  77. length = passwordIsLongEnough(str);
  78. digit = passwordContainsDigit(str);
  79. upperlowercase = passwordHasMixedCase(str);
  80.  
  81. if (length == 1 && digit == 1 && upperlowercase == 1)
  82. {
  83. returnera = 1;
  84. }
  85.  
  86. else
  87. {
  88. returnera = 0;
  89. }
  90.  
  91. if (length == 0)
  92. {
  93. printf("Your password is not long enough\n");
  94. faultcounter++;
  95. }
  96. if (digit == 0)
  97. {
  98. printf("Your password must contain at least one number\n");
  99. faultcounter++;
  100. }
  101.  
  102. if (upperlowercase == 0)
  103. {
  104. printf("Your password must contain one upper case letter and one lower case letter\n");
  105. faultcounter++;
  106. }
  107.  
  108. if (faultcounter > 0)
  109. {
  110. printf("you have %d, faults with your password\n", faultcounter);
  111. }
  112.  
  113. return returnera;
  114. }
  115.  
  116.  
  117. struct account konto(void)
  118. {
  119. struct account userAccount;
  120. printf("Enter username:\n");
  121. scanf("%s", userAccount.user);
  122.  
  123. do
  124. {
  125. do
  126. {
  127. printf("Enter password:\n");
  128. scanf("%s", userAccount.password);
  129. userAccount.control = isSafePassword(userAccount.password);
  130. } while (userAccount.control == 0);
  131. printf("Confirm your password:\n");
  132. scanf("%s", userAccount.newpassword);
  133.  
  134. if (strcmp(userAccount.password, userAccount.newpassword) == 0)
  135. printf("Password confirmed\n");
  136. else
  137. printf("Passwords do not match\n");
  138. } while (strcmp(userAccount.password, userAccount.newpassword) != 0);
  139. return userAccount;
  140.  
  141. }
  142.  
  143.  
  144. int main(void)
  145. {
  146. int konton;
  147. int i;
  148.  
  149. printf("How many useraccounts should be used?\n");
  150. scanf("%d", &konton);
  151. struct account*arr = (struct account*)malloc(sizeof(struct account)*konton);
  152.  
  153. if (arr == NULL)
  154. printf("Not possible to allocate %d elements", konton);
  155. else
  156. for (i = 0; i < konton; i++)
  157. {
  158. arr[i] = konto();
  159. }
  160. printf("Username: \t \t Password: \n");
  161. for (i = 0; i < konton; i++)
  162.  
  163. printf("%s \t \t %s \n", arr[i].user, arr[i].password);
  164. free(arr);
  165.  
  166. return 0;
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement