Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- using namespace std;
- bool digitChecker(string s) {
- int digitCount = 0;
- for (int i = 0; i < s.length(); i++) {
- if (isdigit(s[i])) {
- digitCount++;
- }
- }
- return digitCount >= 2;
- }
- bool symbolChecker(string s) {
- for (int i = 0; i < s.length(); i++) {
- if (!isalnum(s[i])) {
- return false;
- }
- }
- return true;
- }
- bool lengthChecker(string s) {
- return s.length() >= 6 && s.length() <= 10;
- }
- int main() {
- string password;
- cin >> password;
- if (lengthChecker(password) && symbolChecker(password) && digitChecker(password)) {
- cout << "Password is valid" << endl;
- }
- else {
- if (!lengthChecker(password)) {
- cout << "Password must be between 6 and 10 characters" << endl;
- }
- if (!symbolChecker(password)) {
- cout << "Password must consist only of letters and digits" << endl;
- }
- if (!digitChecker(password)) {
- cout << "Password must have at least 2 digits" << endl;
- }
- }
- return 0;
- }
- ИЛИ ТАРИКАТСКАТА С ТЕРНАРЕН ОПЕРАТОР :)
- #include <iostream>
- #include <cctype>
- using namespace std;
- int main() {
- string password;
- cin >> password;
- int symbols = 0, digits = 0;
- for (char p : password) {
- digits += (isdigit(p) ? 1 : 0);
- symbols += (!isalnum(p) ? 1 : 0);
- }
- if (password.length() >= 6 && password.length() <= 10 && symbols == 0 && digits >= 2) {
- cout << "Password is valid\n";
- }
- else {
- cout << (password.length() < 6 || password.length() > 10 ? "Password must be between 6 and 10 characters\n" : "");
- cout << (symbols > 0 ? "Password must consist only of letters and digits\n" : "");
- cout << (digits < 2 ? "Password must have at least 2 digits\n" : "");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement