fork(1) download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4.  
  5. int
  6. password_validate(const char *pass)
  7. {
  8. int upper = 0, lower = 0, digit = 0;
  9. if (pass == NULL || strlen(pass) == 0)
  10. return -1;
  11.  
  12. printf("checking password: ");
  13. do
  14. {
  15. printf("%c", *pass);
  16. if (isupper(*pass))
  17. upper = 1;
  18. if (islower(*pass))
  19. lower = 1;
  20. if (isdigit(*pass))
  21. digit = 1;
  22. } while ((!lower || !upper || !digit) && *(++pass));
  23. printf("\n");
  24. return (*pass != '\0' ? 0 : (upper == 0 ? -2 : (lower == 0 ? -3 : -4)));
  25. }
  26.  
  27. int main(void) {
  28. int err;
  29. const char *pass = "Password";
  30. err = password_validate(pass);
  31. printf("[1][%d]\n", err);
  32.  
  33. pass = "Pass678word";
  34. err = password_validate(pass);
  35. printf("[2][%d]\n", err);
  36.  
  37. pass = "password1";
  38. err = password_validate(pass);
  39. printf("[3][%d]\n", err);
  40.  
  41. pass = "1password";
  42. err = password_validate(pass);
  43. printf("[4][%d]\n", err);
  44.  
  45. pass = "Password1";
  46. err = password_validate(pass);
  47. printf("[5][%d]\n", err);
  48.  
  49. pass = "password1D";
  50. err = password_validate(pass);
  51. printf("[6][%d]\n", err);
  52.  
  53. pass = "pAssword1";
  54. err = password_validate(pass);
  55. printf("[7][%d]\n", err);
  56.  
  57. pass = "1Password";
  58. err = password_validate(pass);
  59. printf("[8][%d]\n", err);
  60.  
  61. pass = "aA1";
  62. err = password_validate(pass);
  63. printf("[9][%d]\n", err);
  64.  
  65. // expected to check "a" and return -2 due to no uppercase.
  66. // ends up checking "aa" and returns 0.
  67. pass = "aa";
  68. err = password_validate(pass);
  69. printf("[10][%d]\n", err);
  70.  
  71. // expected to only check "hi1" and return -2
  72. // ends up checking "hi1N" and returns 0.
  73. pass = "hi1NOTINCLUDED";
  74. err = password_validate(pass);
  75. printf("[11][%d]\n", err);
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 2112KB
stdin
Standard input is empty
stdout
checking password: Password
[1][-4]
checking password: Pass6
[2][0]
checking password: password1
[3][-2]
checking password: 1password
[4][-2]
checking password: Password1
[5][0]
checking password: password1D
[6][0]
checking password: pAssword1
[7][0]
checking password: 1Pa
[8][0]
checking password: aA1
[9][0]
checking password: aa
[10][-2]
checking password: hi1N
[11][0]