Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 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 isItIdentical(char str[])
  71. {
  72. int i;
  73. int returnera = 0;
  74. int name;
  75. int password = 0;
  76. for(i=0; str[i] !='\0'; i++)
  77. {
  78. if((name = password))
  79. returnera = 0;
  80. }
  81. return returnera;
  82. }
  83.  
  84.  
  85. int isSafePassword(char str[])
  86. {
  87. int length;
  88. int digit;
  89. int upperlowercase;
  90. int returnera;
  91. int identical;
  92. length = passwordIsLongEnough(str);
  93. digit = passwordContainsDigit(str);
  94. upperlowercase = passwordHasMixedCase(str);
  95. identical = isItIdentical(str);
  96.  
  97. if(length == 1 && digit == 1 && upperlowercase == 1)
  98. {
  99. returnera = 1;
  100. }
  101.  
  102. else
  103. {
  104. returnera = 0;
  105. }
  106.  
  107. if(length == 0)
  108. printf("Your password is not long enough\n");
  109.  
  110. if(digit == 0)
  111. printf("Your password must contain at least one number\n");
  112.  
  113. if(upperlowercase == 0)
  114. printf("Your password must contain one upper case letter and one lower case letter\n");
  115.  
  116. if(identical==0)
  117. printf("Your password cannot be identical");
  118.  
  119. return returnera;
  120. }
  121.  
  122.  
  123. struct account konto(void)
  124. {
  125. struct account userAccount;
  126. printf("Enter username:\n");
  127. scanf("%s", userAccount.user);
  128.  
  129. do
  130. {
  131. do
  132. {
  133. printf("Enter password:\n");
  134. scanf("%s", userAccount.password);
  135. userAccount.control = isSafePassword(userAccount.password);
  136. }
  137. while(userAccount.control == 0);
  138. printf("Confirm your password:\n");
  139. scanf("%s", userAccount.newpassword);
  140.  
  141. if(strcmp(userAccount.password, userAccount.newpassword)==0)
  142. printf("Password confirmed\n");
  143. else
  144. printf("Passwords do not match\n");
  145. }
  146. while(strcmp(userAccount.password, userAccount.newpassword)!=0);
  147. return userAccount;
  148.  
  149. }
  150.  
  151.  
  152. int main (void)
  153. {
  154. int konton;
  155. int i;
  156.  
  157. printf("How many useraccounts should be used?\n");
  158. scanf("%d", &konton);
  159. struct account*arr = (struct account*)malloc(sizeof(struct account)*konton);
  160.  
  161. if(arr==NULL)
  162. printf("Not possible to allocate %d elements", konton);
  163. else
  164. for(i=0; i<konton; i++)
  165. {
  166. arr[i] = konto();
  167. }
  168. printf("Username: \t \t Password: \n");
  169. for (i=0; i<konton; i++)
  170.  
  171. printf("%s \t \t %s \n", arr[i].user, arr[i].password);
  172. free(arr);
  173.  
  174. return 0;
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement