LAB 10.1 Character Testing and String Validation The American Equities investment company offers a wide range of investment opportunities ranging from mutual funds to bonds. Investors can check the val- ue of their portfolio from the American Equities’ web page. Information about personal portfolios is protected via encryption and can only be accessed using a password. The American Equities company requires that a password consist of 8 characters, 5 of which must be letters and the other 3 digits. The letters and dig- its can be arranged in any order. For example, rt56AA7q 123actyN 1Lo0Dwa9 myNUM741 are all valid passwords. However, the following are all invalid: the476NEw // It contains more than 8 characters (also more than 5 // letters) be68moon // It contains less than 3 digits. $retrn99 // It contains only 2 digits and has an invalid character (‘$’) American Equities needs a program for their web page that determines whether or not an entered password is valid. The program american_equities.cpp from the Lab 10 folder performs this task. The code is the following: // This program tests a password for the American Equities // web page to see if the format is correct // Place Your Name Here #include #include #include using namespace std; //function prototypes bool testPassWord(char[]); int countLetters(char*); int countDigits(char*); int main() { char passWord[20]; cout << "Enter a password consisting of exactly 5 " << "letters and 3 digits:" << endl; cin.getline(passWord,20); if (testPassWord(passWord)) cout << "Please wait - your password is being verified" << endl; else { } cout << "Invalid password. Please enter a password " << "with exactly 5 letters and 3 digits" << endl; cout << "For example, my37RuN9 is valid" << endl; // Fill in the code that will call countLetters and // countDigits and will print to the screen both the number of // letters and digits contained in the password. return 0; } //************************************************************** // testPassWord // // task: determines if the word in the // character array passed to it, contains // exactly 5 letters and 3 digits. // data in: a word contained in a character array // data returned: true if the word contains 5 letters & 3 // digits, false otherwise // //************************************************************** bool testPassWord(char custPass[]) { int numLetters, numDigits, length; length = strlen(custPass); numLetters = countLetters(custPass); numDigits = countDigits(custPass); if (numLetters == 5 && numDigits == 3 && length == 8 ) return true; else return false; } // the next 2 functions are from Sample Program 10.5 //************************************************************** // countLetters // // task: counts the number of letters (both // capital and lower case)in the string // data in: a string // data returned: the number of letters in the string // //************************************************************** int countLetters(char *strPtr) { int occurs = 0; while(*strPtr != '\0') { if (isalpha(*strPtr)) occurs++; strPtr++; } return occurs; } //************************************************************** // countDigits // // task: counts the number of digits in the string // data in: a string // data returned: the number of digits in the string // //************************************************************** int countDigits(char *strPtr) { int occurs = 0; while(*strPtr != '\0') { if (isdigit(*strPtr)) // isdigit determines if // the character is a digit occurs++; strPtr++; } return occurs; } Exercise 1: Fill in the code in bold and then run the program several times with both valid and invalid passwords. Read through the program and make sure you understand the logic of the code. Exercise 2: Alter the program so that a valid password consists of 10 charac- ters, 6 of which must be digits and the other 4 letters. Exercise 3: Adjust your program from Exercise 2 so that only lower case letters are allowed for valid passwords.

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter7: File Handling And Applications
Section: Chapter Questions
Problem 6PE
icon
Related questions
Topic Video
Question

LAB 10.1 Character Testing and String Validation
The American Equities investment company offers a wide range of investment
opportunities ranging from mutual funds to bonds. Investors can check the val-
ue of their portfolio from the American Equities’ web page. Information about
personal portfolios is protected via encryption and can only be accessed using
a password. The American Equities company requires that a password consist of
8 characters, 5 of which must be letters and the other 3 digits. The letters and dig-
its can be arranged in any order. For example,
rt56AA7q
123actyN
1Lo0Dwa9
myNUM741
are all valid passwords. However, the following are all invalid:
the476NEw // It contains more than 8 characters (also more than 5
// letters)
be68moon // It contains less than 3 digits.
$retrn99 // It contains only 2 digits and has an invalid character (‘$’)

American Equities needs a program for their web page that determines whether or
not an entered password is valid. The program american_equities.cpp from the
Lab 10 folder performs this task. The code is the following:
// This program tests a password for the American Equities
// web page to see if the format is correct
// Place Your Name Here
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
//function prototypes
bool testPassWord(char[]);
int countLetters(char*);
int countDigits(char*);
int main()
{
char passWord[20];
cout << "Enter a password consisting of exactly 5 "
<< "letters and 3 digits:" << endl;
cin.getline(passWord,20);
if (testPassWord(passWord))
cout << "Please wait - your password is being verified" << endl;
else
{
}
cout << "Invalid password. Please enter a password "
<< "with exactly 5 letters and 3 digits" << endl;
cout << "For example, my37RuN9 is valid" << endl;
// Fill in the code that will call countLetters and
// countDigits and will print to the screen both the number of
// letters and digits contained in the password.
return 0;
}

//**************************************************************
// testPassWord
//
// task: determines if the word in the
// character array passed to it, contains
// exactly 5 letters and 3 digits.
// data in: a word contained in a character array
// data returned: true if the word contains 5 letters & 3
// digits, false otherwise
//
//**************************************************************
bool testPassWord(char custPass[])
{
int numLetters, numDigits, length;
length = strlen(custPass);
numLetters = countLetters(custPass);
numDigits = countDigits(custPass);
if (numLetters == 5 && numDigits == 3 && length == 8 )
return true;
else
return false;
}
// the next 2 functions are from Sample Program 10.5
//**************************************************************
// countLetters
//
// task: counts the number of letters (both
// capital and lower case)in the string
// data in: a string
// data returned: the number of letters in the string
//
//**************************************************************
int countLetters(char *strPtr)
{
int occurs = 0;
while(*strPtr != '\0')
{
if (isalpha(*strPtr))
occurs++;
strPtr++;
}
return occurs;
}

//**************************************************************
// countDigits
//
// task: counts the number of digits in the string
// data in: a string
// data returned: the number of digits in the string
//
//**************************************************************
int countDigits(char *strPtr)
{
int occurs = 0;
while(*strPtr != '\0')
{
if (isdigit(*strPtr)) // isdigit determines if
// the character is a digit
occurs++;
strPtr++;
}
return occurs;
}
Exercise 1: Fill in the code in bold and then run the program several times
with both valid and invalid passwords. Read through the program and
make sure you understand the logic of the code.
Exercise 2: Alter the program so that a valid password consists of 10 charac-
ters, 6 of which must be digits and the other 4 letters.
Exercise 3: Adjust your program from Exercise 2 so that only lower case
letters are allowed for valid passwords.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 9 images

Blurred answer
Knowledge Booster
Instruction Format
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
Operations Research : Applications and Algorithms
Operations Research : Applications and Algorithms
Computer Science
ISBN:
9780534380588
Author:
Wayne L. Winston
Publisher:
Brooks Cole