122k views
1 vote
Declare a Boolean variable named goodPassword. Use goodPassword to output "Valid" if passwordStr contains at least 4 letters and passwordStr's length is less than or equal to 7, and "Invalid" otherwise.

Ex: If the input is xmz97H2, then the output is:

Valid

Ex: If the input is 4rY2D0QR, then the output is:

Invalid

Note: isalpha() returns true if a character is alphabetic, and false otherwise. Ex: isalpha('a') returns true. isalpha('8') returns false.

}


#include
using namespace std;

int main() {
string passwordStr;
bool goodPassword;
cin >> passwordStr;
int letters = 0;


getline(cin, passwordStr);

for (int i = 0; i < passwordStr.length(); i++) {
if (isalpha(passwordStr[i])) {
letters++;
}
}

if (letters == 4 && passwordStr.length() <= 7) {
goodPassword = true;
}
else {
goodPassword = false;
}


if (goodPassword) {
cout << "Valid" << endl;
}
else {
cout << "Invalid" << endl;
}

return 0;
}

how do i fix this

1 Answer

3 votes

Answer:

c++

Step-by-step explanation:

User Nisarg Bhavsar
by
7.2k points