//p.224 C++로 시작하는 객체지향 프로그래밍 예제 5.57
/* Q. 웹사이트에 따라 비밀번호에 어떤 법칙을 요구하는 경우가 있다. 다음과 같이 비밀번호를 만들어야 한다고 하자.
- 비밀번호는 적어도 8개의 문자여야 한다.
- 비밀번호는 문자와 숫자로만 구성되어야 한다.
- 비밀번호는 적어도 두 개의 숫자가 포함되어야 한다.*/
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main(){
int length, digitCnt = 0;
string pwd;
cin >> pwd;
length = pwd.length(); //len=strlen(pw)
if (length < 8) cout << "invalid password" << endl;
else{
for(int i = 0; i < length; i++) {
if (isalnum(pwd[i])==false) {
cout << "Invalid password" << endl;
break;
}
else if (isdigit(pwd[i]==true))
digitCnt++;
}
if (digitCnt < 2)
cout << "Invalid password" << endl;
else cout << "Valid password" << endl;
}
system("pause");
return 0;
}
댓글 없음:
댓글 쓰기