Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
130
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<stdlib.h>
  3. #include<time.h>
  4. #include<ctype.h>
  5. #include<string.h>
  6.  
  7. int passwordIsLongEnough(char str[])
  8. {
  9. int i, Count = 0, siffra;
  10.  
  11. for (i = 0; str[i] != '\0'; i++)
  12.  
  13. Count++;
  14.  
  15. if (Count < 8)
  16. {
  17. siffra = 0;
  18. }
  19. else if (Count >= 8)
  20. {
  21. siffra = 1;
  22. }
  23. return siffra;
  24. }
  25.  
  26. int passwordContainsDigit(char str[])
  27. {
  28. int i, digitCount = 0, digits;
  29. for (i = 0; str[i] != '\0'; i++)
  30. if (isdigit(str[i]))
  31. digitCount++;
  32.  
  33. if (digitCount < 1)
  34. {
  35. digits = 0;
  36. }
  37. else
  38. {
  39. digits = 1;
  40. }
  41. return digits;
  42. }
  43.  
  44. int passwordHasMixedCase(char str[])
  45. {
  46. char i, upper = 0, lower = 0, mixed;
  47. for (i = 0; str[i] != '\0'; i++)
  48. {
  49. if (isupper(str[i]))
  50. upper++;
  51. if (islower(str[i]))
  52. lower++;
  53. }
  54. if (upper >0 && lower >0)
  55. mixed = 1;
  56. else
  57. mixed = 0;
  58. return mixed;
  59. }
  60.  
  61. int containsFourLetters(char str[])
  62. {
  63. int i, letterCount = 0, letters;
  64. for (i = 0; str[i] != '\0'; i++);
  65. {
  66. if (isalpha(str[i]))
  67. {
  68. letterCount++;
  69. }
  70. }
  71.  
  72. if (letterCount < 4)
  73. {
  74. letters = 0;
  75. }
  76. else
  77. {
  78. letters = 1;
  79. }
  80.  
  81. return letters;
  82. }
  83.  
  84. int isSafePassword(char password[])
  85. {
  86. int length, digits, mixed, letters, SAFE;
  87. length = passwordIsLongEnough(password);
  88. digits = passwordContainsDigit(password);
  89. mixed = passwordHasMixedCase(password);
  90. letters = containsFourLetters(password);
  91.  
  92. if (length == 1 && digits == 1 && mixed == 1 && letters==0)
  93. {
  94. SAFE = 1;
  95. printf("This password is safe!");
  96. }
  97. else
  98. {
  99. SAFE = 0;
  100. printf("Your password is not safe!");
  101.  
  102. if (length == 0)
  103. {
  104. printf("\nYou need to have at least 8 character!");
  105. }
  106. if (digits == 0)
  107. {
  108. printf("\nYou need to have at least one digit!");
  109. }
  110. if (mixed == 0)
  111. {
  112. printf("\nYou need to have at least one small and one big letter!");
  113. }
  114. if (letters == 1)
  115. {
  116. printf("\nYou passsword contains more than 3 letters!");
  117. }
  118. }
  119. return SAFE;
  120. }
  121.  
  122.  
  123.  
  124. struct account
  125. {
  126. char name[30];
  127. char password[30];
  128. char newpassword[30];
  129. int SAFE;
  130. };
  131.  
  132. void capitalize(char Name[])
  133. {
  134.  
  135. Name[0] = toupper(Name[0]);
  136.  
  137.  
  138. }
  139.  
  140.  
  141. struct account EnterAccount(void)
  142. {
  143. struct account person;
  144. printf("\n\nEnter your username:");
  145. scanf("%s", person.name);
  146.  
  147.  
  148. capitalize(person.name);
  149.  
  150.  
  151. do
  152. {
  153. do
  154. {
  155. printf("\nEnter password:");
  156. scanf("%s", person.password);
  157. person.SAFE = isSafePassword(person.password);
  158. } while (person.SAFE == 0);
  159.  
  160. printf("\nEnter the same password");
  161. scanf("%s", person.newpassword);
  162.  
  163. if (strcmp(person.password, person.newpassword) != 0)
  164. {
  165. printf("The passwords are not matching!");
  166. }
  167. else
  168. {
  169. printf("Your account is accepted!");
  170. printf("\nThe password %s is associated whith user :%s", person.password, person.name);
  171. }
  172. }while (strcmp(person.password, person.newpassword) != 0);
  173.  
  174. return person;
  175. }
  176.  
  177. int main(void)
  178. {
  179. int amount;
  180. int i;
  181. printf("How many accounts do you want to enter?");
  182. scanf("%d", &amount);
  183. account* arr = (account*)malloc(sizeof(account)*amount);
  184.  
  185. if(arr==NULL)
  186. {
  187. printf("could not allocate %d elements!\n ", amount);
  188. }
  189.  
  190. else
  191. {
  192. for (i = 0; i < amount; i++)
  193. {
  194. arr[i] = EnterAccount();
  195.  
  196. }
  197.  
  198. printf("\n\nUsername");
  199. printf("\tPassword");
  200.  
  201. for (i = 0; i < amount; i++)
  202. {
  203. printf("\n%s", arr[i].name);
  204. printf("\t\t%s", arr[i].password);
  205. }
  206.  
  207. free(arr);
  208. }
  209.  
  210. return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement