fork download
  1. //Sam Partovi CS1A Ch. 10, P.590, #12
  2. /*******************************************************************************
  3. * DETERMINE PASSWORD VALIDITY
  4. * ____________________________________________________________
  5. * This program accepts a given password and determines its validity based on
  6. * specified minimum password requirements, such as character and digit count.
  7. * ____________________________________________________________
  8. *INPUT
  9. * userPassword : Given user password
  10. * minLength : Minimum length of password
  11. * minUppercase : Minimum number of uppercase letters
  12. * minLowercase : Minimum number of lowercase letters
  13. * minDigits : Minimum number of digits
  14. *OUTPUT
  15. * Messages indicating whether the password meets the requirements or not.
  16. *******************************************************************************/
  17.  
  18. #include <iostream>
  19. #include <string>
  20. #include <cctype>
  21. using namespace std;
  22.  
  23. //Structure to define password requirements
  24. struct Password {
  25. int minLength; //INPUT - Minimum length of password
  26. int minUppercase; //INPUT - Minimum number of uppercase letters
  27. int minLowercase; //INPUT - Minimum number of lowercase letters
  28. int minDigits; //INPUT - Minimum number of digits
  29. };
  30.  
  31. //Function prototype
  32. void PasswordVerifyFunc(const string& userPassword,
  33. const Password& requirements);
  34.  
  35. int main() {
  36. //Password requirements
  37. Password requirements = {6, 1, 1, 1};
  38. string userPassword;
  39.  
  40. //Prompt for a password
  41. cout << "Enter a password: ";
  42. cin >> userPassword;
  43. cout << endl;
  44.  
  45. //Verify the password
  46. PasswordVerifyFunc(userPassword, requirements);
  47.  
  48. return 0;
  49. }
  50.  
  51. /*******************************************************************************
  52. * Definition of PasswordVerifyFunc function:
  53. * This function verifies the given password in userPassword against the
  54. * specified requirements in the Password structure.
  55. *******************************************************************************/
  56. void PasswordVerifyFunc(const string& userPassword,
  57. const Password& requirements) {
  58.  
  59. int charCount = userPassword.length(); //Get the length of the password
  60. int uppercaseCount = 0; //Count uppercase letters
  61. int lowercaseCount = 0; //Count lowercase letters
  62. int digitCount = 0; //Count digits
  63.  
  64. //Iterate through the password to count character types
  65. for (char c : userPassword) {
  66. if (isupper(c)) uppercaseCount++;
  67. if (islower(c)) lowercaseCount++;
  68. if (isdigit(c)) digitCount++;
  69. }
  70.  
  71. //Check requirements and display results
  72. bool isValid = true;
  73.  
  74. if (charCount < requirements.minLength) {
  75. cout << "Password must be at least " << requirements.minLength
  76. << " characters long.\n";
  77. isValid = false;
  78. }
  79. if (uppercaseCount < requirements.minUppercase) {
  80. cout << "Password must contain at least " << requirements.minUppercase
  81. << " uppercase letter(s).\n";
  82. isValid = false;
  83. }
  84. if (lowercaseCount < requirements.minLowercase) {
  85. cout << "Password must contain at least " << requirements.minLowercase
  86. << " lowercase letter(s).\n";
  87. isValid = false;
  88. }
  89. if (digitCount < requirements.minDigits) {
  90. cout << "Password must contain at least " << requirements.minDigits
  91. << " digit(s).\n";
  92. isValid = false;
  93. }
  94.  
  95. //If all requirements are met
  96. if (isValid) {
  97. cout << "Password is valid.\n";
  98. }
  99. }
Success #stdin #stdout 0s 5288KB
stdin
pass
stdout
Enter a password: 
Password must be at least 6 characters long.
Password must contain at least 1 uppercase letter(s).
Password must contain at least 1 digit(s).