46.0k views
3 votes
Declare a Boolean variable named isValidPasswd. Use isValidPasswd to output "Valid" if codeStr contains no more than 4 letters and codeStr's length is less than 9, and "Invalid" otherwise.

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

Valid

Ex: If the input is n84xfj061, 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.

please help in c++

User Jakehallas
by
7.7k points

2 Answers

2 votes

Answer:

c++

Step-by-step explanation:

User Tenhjo
by
7.8k points
1 vote

Answer: #include <iostream>

#include <string>

using namespace std;

int main() {

string codeStr = "l7J45s6f";

bool isValidPasswd = true;

int letterCount = 0;

for (char c : codeStr) {

if (isalpha(c)) {

letterCount++;

}

if (letterCount > 4 || codeStr.length() >= 9) {

isValidPasswd = false;

break;

}

}

if (isValidPasswd) {

cout << "Valid" << endl;

} else {

cout << "Invalid" << endl;

}

return 0;

}

Explanation: In this usage, we to begin with characterize a codeStr variable with the input string, and a isValidPasswd variable initialized to genuine. We at that point circle through each character in codeStr, increasing a letterCount variable in case the current character is alphabetic. In case the letterCount gets to be more noteworthy than 4, or in the event that the length of codeStr is more prominent than or break even with to 9, we set isValidPasswd to wrong and break out of the circle.

At last, we yield "Substantial" in case isValidPasswd is genuine, and "Invalid" something else.

User Daydayup
by
8.7k points