Advertisement
Spocoman

07. Password Validator

Nov 17th, 2023 (edited)
1,473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3.  
  4. using namespace std;
  5.  
  6. bool digitChecker(string s) {
  7.     int digitCount = 0;
  8.     for (int i = 0; i < s.length(); i++) {
  9.         if (isdigit(s[i])) {
  10.             digitCount++;
  11.         }
  12.     }
  13.     return digitCount >= 2;
  14. }
  15.  
  16. bool symbolChecker(string s) {
  17.     for (int i = 0; i < s.length(); i++) {
  18.         if (!isalnum(s[i])) {
  19.             return false;
  20.         }
  21.     }
  22.     return true;
  23. }
  24.  
  25. bool lengthChecker(string s) {
  26.     return s.length() >= 6 && s.length() <= 10;
  27. }
  28.  
  29. int main() {
  30.     string password;
  31.     cin >> password;
  32.  
  33.     if (lengthChecker(password) && symbolChecker(password) && digitChecker(password)) {
  34.         cout << "Password is valid" << endl;
  35.     }
  36.     else {
  37.         if (!lengthChecker(password)) {
  38.             cout << "Password must be between 6 and 10 characters" << endl;
  39.         }
  40.         if (!symbolChecker(password)) {
  41.             cout << "Password must consist only of letters and digits" << endl;
  42.         }
  43.         if (!digitChecker(password)) {
  44.             cout << "Password must have at least 2 digits" << endl;
  45.         }
  46.     }
  47.     return 0;
  48. }
  49.  
  50. ИЛИ ТАРИКАТСКАТА С ТЕРНАРЕН ОПЕРАТОР :)
  51.  
  52. #include <iostream>
  53. #include <cctype>
  54.  
  55. using namespace std;
  56.  
  57. int main() {
  58.     string password;
  59.     cin >> password;
  60.  
  61.     int symbols = 0, digits = 0;
  62.  
  63.     for (char p : password) {
  64.         digits += (isdigit(p) ? 1 : 0);
  65.         symbols += (!isalnum(p) ? 1 : 0);
  66.     }
  67.  
  68.     if (password.length() >= 6 && password.length() <= 10 && symbols == 0 && digits >= 2) {
  69.         cout << "Password is valid\n";
  70.     }
  71.     else {
  72.         cout << (password.length() < 6 || password.length() > 10 ? "Password must be between 6 and 10 characters\n" : "");
  73.         cout << (symbols > 0 ? "Password must consist only of letters and digits\n" : "");
  74.         cout << (digits < 2 ? "Password must have at least 2 digits\n" : "");
  75.     }
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement