Guest User

Untitled

a guest
Jan 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define TRUE 1
  5. #define FALSE 0
  6.  
  7. int apply_password_policy(char password[], int len){
  8.  
  9. int i,j,k=0;
  10.  
  11. /* Policy1:
  12. * Space not allowed in the password
  13. */
  14. for (i=0; i<len; i++){
  15. if (password[i] == ' '){
  16. return FALSE;
  17. }
  18. }
  19.  
  20. /* Policy2:
  21. * Password should be between 1 to 10 Uppercase letters
  22. */
  23. for (i=0; i<len; i++){
  24. if (!isupper(password[i])){
  25. return FALSE;
  26. }
  27. }
  28.  
  29. /* Policy3:
  30. * Same character should not appear together.
  31. */
  32. for (i=0; i<len; i++) {
  33. if ( i+1 < len) {
  34. if (password[i] == password[i+1])
  35. return FALSE;
  36. }
  37. }
  38.  
  39. /* Policy4:
  40. * Same string shold not appear together.
  41. */
  42. for (i=0; i<len; i++){
  43. for (j=i+1; j<len; j++) {
  44. if(password[i] == password[j]) {
  45. if ( k == 0) {
  46. /* store the J value when first character match happens
  47. */
  48. k=j;
  49. break;
  50. }
  51.  
  52. /* Next subscript of 'i' is equal to the first charater match happens at 'J'
  53. * Meaning, substring is together.
  54. */
  55. if ( i+1 == k)
  56. return FALSE;
  57. }
  58. }
  59. }
  60.  
  61. return TRUE;
  62. }
  63.  
  64. int main(){
  65.  
  66. char password[10];
  67. int len=0;
  68.  
  69. printf("Enter the password: ");
  70.  
  71. while (1) {
  72. password[len]=getc(stdin);
  73. if (password[len]=='\n'){
  74. password[len]='\0';
  75. break;
  76. }
  77. len++;
  78. }
  79.  
  80. if (apply_password_policy(password, len))
  81. printf ("Password Accepted. \n");
  82. else
  83. printf("Password Rejected. \n");
  84.  
  85. return 0;
  86. }
Add Comment
Please, Sign In to add comment