Advertisement
TrickmanOff

A2

Sep 8th, 2020
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. // if string 's' contains symbol for which 'cond_func' returns true
  8. bool is_in_string(const string& s, int(*cond_func)(int)) {
  9.     auto it = find_if(s.begin(), s.end(),
  10.                       [&](const char ch) {return cond_func(ch); });
  11.     return it != s.end();
  12. }
  13.  
  14. // if char 'ch' is not alphanumeric
  15. int is_not_alnum(int ch) {
  16.     return !isalnum(ch);
  17. }
  18.  
  19. bool is_password_correct(const string& password) {
  20.     // if all symbols are ASCII [33:126]
  21.     for (const auto& ch : password) {
  22.         if (ch < 33 || ch > 126) {
  23.             return false;
  24.         }
  25.     }
  26.     // if length of password is [8:14]
  27.     if (!(password.length() >= 8 && password.length() <= 14)) {
  28.         return false;
  29.     }
  30.     // check for >=3 of 4 classes (uppercase, lowercase, digit, other symbols)
  31.     int class_cnt = 0;
  32.     class_cnt += is_in_string(password, isupper);
  33.     class_cnt += is_in_string(password, islower);
  34.     class_cnt += is_in_string(password, isdigit);
  35.     class_cnt += is_in_string(password, is_not_alnum);
  36.     return class_cnt >= 3;
  37. }
  38.  
  39. int main() {
  40.     string password;
  41.     getline(cin, password);
  42.     cout << (is_password_correct(password) ? "YES" : "NO") << '\n';
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement