fork download
  1. // Diego Martinez CSC5 Chapter 10, P.590, #12
  2. /*******************************************************************************
  3. * VALIDATE PASSWORD
  4. * ______________________________________________________________________________
  5. * This program asks the user to enter a password and verifies that it meets
  6. * the required criteria:
  7. *
  8. * - At least 6 characters long
  9. * - Contains at least one uppercase letter
  10. * - Contains at least one lowercase letter
  11. * - Contains at least one digit
  12. *
  13. * The program displays messages explaining why the password is invalid if
  14. * it does not meet the requirements.
  15. *
  16. * ______________________________________________________________________________
  17. * INPUT
  18. *
  19. * User password
  20. *
  21. * OUTPUT
  22. *
  23. * Validation results for the password
  24. *
  25. *******************************************************************************/
  26.  
  27. #include <iostream>
  28. #include <cstring>
  29. #include <cctype>
  30. using namespace std;
  31.  
  32. // Function prototype
  33. bool verifyPassword(const char *);
  34.  
  35. int main()
  36. {
  37. // Variable declaration
  38. char password[100];
  39.  
  40. // User input
  41. cout << "Enter a password: ";
  42. cin.getline(password, 100);
  43.  
  44. // Verify password
  45. if (verifyPassword(password))
  46. {
  47. cout << "Password is valid." << endl;
  48. }
  49. else
  50. {
  51. cout << "Password is invalid." << endl;
  52. }
  53.  
  54. return 0;
  55. }
  56.  
  57. // Function definition
  58. bool verifyPassword(const char *str)
  59. {
  60. int length = strlen(str);
  61.  
  62. bool hasUpper = false;
  63. bool hasLower = false;
  64. bool hasDigit = false;
  65.  
  66. // Check length
  67. if (length < 6)
  68. {
  69. cout << "Password must be at least 6 characters long." << endl;
  70. }
  71.  
  72. // Check characters
  73. for (int i = 0; i < length; i++)
  74. {
  75. if (isupper(*(str + i)))
  76. {
  77. hasUpper = true;
  78. }
  79.  
  80. if (islower(*(str + i)))
  81. {
  82. hasLower = true;
  83. }
  84.  
  85. if (isdigit(*(str + i)))
  86. {
  87. hasDigit = true;
  88. }
  89. }
  90.  
  91. // Display missing requirements
  92. if (!hasUpper)
  93. {
  94. cout << "Password must contain at least one uppercase letter." << endl;
  95. }
  96.  
  97. if (!hasLower)
  98. {
  99. cout << "Password must contain at least one lowercase letter." << endl;
  100. }
  101.  
  102. if (!hasDigit)
  103. {
  104. cout << "Password must contain at least one digit." << endl;
  105. }
  106.  
  107. // Return result
  108. if (length >= 6 && hasUpper && hasLower && hasDigit)
  109. {
  110. return true;
  111. }
  112. else
  113. {
  114. return false;
  115. }
  116. }
Success #stdin #stdout 0s 5316KB
stdin
Diego55
stdout
Enter a password: Password is valid.