In: Computer Science
Instructions (In C++ Please)
Verification
This programming lab makes use of an array of characters. The program will validate the characters in the array to see if they meets a set of requirements.
Requirements:
Must be at least 6 characters long (but not limited to 6, can be greater)
Contains one upper case letter
Contains one lower case letter
Contains one digit
Complete the code attached. Must use pointer while checking the characters.
Download Source Lab 3 File:
#include using namespace std;
// Global constants const int SIZE = 80;
// The maximum size of the array const int MIN = 6;
// The minimum number characters
// Function prototypes void displayRequirements();
void displayResult(char[]);
int main()
{
char cstring[SIZE];
displayRequirements();
cout << "Enter a password: ";
cin.getline(cstring, SIZE);
displayResult(cstring);
return 0;
}
void displayRequirements()
{
// Display the password requirements.
cout << "Password Requirments:\n"
<< " - The password should be at least "
<< MIN << " characters long.\n"
<< " - The password should contain at least one uppercase\n"
<< " and at least one lowercase letter.\n"
<< " - The password should have at least one digit.\n\n";
}
void displayResult(char str[])
{
bool length, upper, lower, digit;
length = upper = lower = digit = false;
int lengthCount = 0;
}
The completed C++ code is provided below, please comment if any doubts:
Note: The given code is completed below, the added portion is highlighted
C++ Code:
#include<iostream>
using namespace std;
// Global constants
const int SIZE = 80;
// The maximum size of the array
const int MIN = 6;
// The minimum number characters
// Function prototypes
void displayRequirements();
void displayResult(char[]);
int main()
{
char cstring[SIZE];
displayRequirements();
cout << "Enter a password: ";
cin.getline(cstring, SIZE);
displayResult(cstring);
return 0;
}
void displayRequirements()
{
// Display the password requirements.
cout << "Password Requirments:\n"
<< " - The password should be at least "
<< MIN << " characters long.\n"
<< " - The password should contain at least one uppercase\n"
<< " and at least one lowercase letter.\n"
<< " - The password should have at least one digit.\n\n";
}
void displayResult(char str[])
{
bool length, upper, lower, digit;
length = upper = lower = digit = false;
int lengthCount = 0;
char *pw = str;
//while there is characters in the
string
while(*pw != '\0')
{
//check whether the character is
lower case letter
if(*pw >='a' && *pw
<= 'z')
{
lower=true;
}
//check whether the
character is upper case letter
if(*pw >='A' && *pw
<= 'Z')
{
upper=true;
}
//check whether the
character is a digit
if(*pw >='0' && *pw
<= '9')
{
digit=true;
}
//update the pointer to
next
pw++;
lengthCount++;
}
//check the length requirement
if(lengthCount>=6)
{
length = true;
}
//check whether all the requirements
satisfied
if(length && upper && lower &&
digit)
{
//display the result
cout<<" The password is
valid!!!!"<<endl;
}
else
{
cout<<" The password not
satisfying the requirements!!!!"<<endl;
}
}
Sample Outputs: