Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. int passwordIsLongEnough(char str[]) /* funk.1 */
  7. {
  8.  
  9. int i;
  10. int counter = 0;
  11. int returnera;
  12. for (i = 0; str[i] != '\0'; ++i)
  13. counter++;
  14. if (counter < 8)
  15. returnera = 0;
  16. else
  17. returnera = 1;
  18. return returnera;
  19.  
  20. }
  21.  
  22. int passwordContainsDigit(char str[]) /* funk.2 */
  23. {
  24. int i, digit = 0, returnera;
  25.  
  26. for (i = 0; str[i] != '\0'; ++i)
  27. if (isdigit(str[i]))
  28. digit++;
  29. if (digit > 0)
  30.  
  31. returnera = 1;
  32.  
  33. else
  34.  
  35. returnera = 0;
  36.  
  37. return returnera;
  38. }
  39. int passwordHasMixedCase(char str[])
  40. {
  41. int i, returnera = 0, upper = 0, lower = 0;
  42. for (i = 0; str[i] != '\0'; i++)
  43. {
  44. if (islower(str[i]))
  45. {
  46. lower++;
  47. }
  48. if (isupper(str[i]))
  49. {
  50. upper++;
  51. }
  52.  
  53. if (upper > 0 && lower > 0)
  54. {
  55. returnera = 1;
  56. }
  57. else
  58. {
  59. returnera = 0;
  60. }
  61. }
  62. return returnera;
  63. }
  64. void capitalize(char Name[])
  65. {
  66.  
  67. Name[0] = toupper(Name[0]);
  68.  
  69.  
  70. }
  71.  
  72.  
  73.  
  74. int isSafePassword(char password[])
  75. {
  76. int length, digit, upperLowerCase, returnera;
  77. int faultCounter = 0;
  78. length = passwordIsLongEnough(password);
  79. digit = passwordContainsDigit(password);
  80. upperLowerCase = passwordHasMixedCase(password);
  81.  
  82. if (length == 1 && digit == 1 && upperLowerCase == 1)
  83. {
  84. returnera = 1;
  85. }
  86. else
  87. {
  88. returnera = 0;
  89. }
  90. if (length == 0)
  91. {
  92. printf("Your password is not long enough, it has to be at least 8 characters \n");
  93. faultCounter++;
  94. }
  95. if (digit == 0)
  96. {
  97. printf("You password has to contain at least one digit\n");
  98. faultCounter++;
  99. }
  100. if (upperLowerCase == 0)
  101. {
  102. printf("Your password has to have at least one upper case letter and at least one lower case letter \n");
  103. faultCounter++;
  104. }
  105. if (faultCounter > 0)
  106. {
  107. printf("You have %d, faults in yur password\n", faultCounter);
  108. }
  109.  
  110. return returnera;
  111.  
  112. }
  113.  
  114.  
  115. struct account
  116. {
  117. char username[30];
  118. char password[30];
  119. char newPassword[30];
  120. int passwordControl;
  121.  
  122. };
  123.  
  124. struct account konto(void)
  125. {
  126. struct account userAccount;
  127.  
  128. printf("Enter a username: \n");
  129. scanf("%s", userAccount.username);
  130. capitalize(userAccount.username);
  131. do
  132. {
  133. do
  134. {
  135.  
  136. printf("Enter a password:\n");
  137. scanf("%s", userAccount.password);
  138. userAccount.passwordControl = isSafePassword(userAccount.password);
  139.  
  140. } while (userAccount.passwordControl == 0);
  141. printf("Please confirm your password:\n");
  142. scanf("%s", userAccount.newPassword);
  143.  
  144. if (strcmp(userAccount.password, userAccount.newPassword) == 0)
  145. {
  146. printf("The password is confirmed\n");
  147. printf("The password %s is associated whith user :%s\n", userAccount.password, userAccount.username);
  148. }
  149. else
  150. printf("The passwords do not match\n");
  151. } while (strcmp(userAccount.password, userAccount.newPassword) != 0);
  152.  
  153.  
  154. return userAccount;
  155. }
  156.  
  157.  
  158. int main(void)
  159. {
  160. int antalKonton;
  161. int i;
  162.  
  163. printf("How many useraccounts do you want to enter? \n");
  164. scanf("%d", &antalKonton);
  165.  
  166. struct account* arr = (struct account*) malloc(sizeof(struct account)*antalKonton);
  167.  
  168. if (arr == NULL)
  169. printf("Could not allocate %d elements", antalKonton);
  170. else
  171. {
  172. for (i = 0; i < antalKonton; i++)
  173. {
  174. arr[i] = konto();
  175. }
  176. printf("Username: \t \t password: \n");
  177. for (i = 0; i < antalKonton; i++)
  178.  
  179. {
  180.  
  181. printf("%s \t \t %s \n", arr[i].username, arr[i].password);
  182. }
  183. free(arr);
  184.  
  185. }
  186. return 0;
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement