In: Computer Science
Class Instructions
You will create a password verification program using C-strings and character testing.
The user will enter a password as a C-string. (Character array). You must test that the password contains at least:
I need help in setting this program up.
#include <iostream>
#include <cctype>
using namespace std;
int main() {
char input;
cout << "Enter any character: ";
cin.get(input);
cout << "The character you entered is: " << input << endl;
if (isalpha(input))
cout << "That's an alphabetic character.\n";
if (isdigit(input))
cout << "That's a numeric digit.\n";
if (islower(input))
cout << "The letter you entered is lowercase.\n";
if (isupper(input))
cout << "The letter you entered is uppercase.\n";
if (isspace(input))
cout << "That's a whitespace character.\n";
return 0;
}
#include <iostream>
#include <cctype>
#include <cstring>
using namespace std;
enum PWSIZE //Array Size
{
PASSWORD_SIZE = 20
};
//Function Prototype
int testNum(char []);
int main()
{
char password[PASSWORD_SIZE]; //To hold password
int length;
int flag=0;
length = strlen(password);
cout<< "Please enter a password with at least 6 characters.\n";
while(1)
{
//Get the password.
do{
if(flag==0)
cout << "Enter your password: ";
else
cout << "Re-enter your password: ";
cin.getline(password, PASSWORD_SIZE);
length = strlen(password);
}while(length < 6);
//Call function.
if(testNum(password))
{
cout<<"The entered password is correect pattern.\n" ;
break;
} //if return 1 pass below
else
{
flag=1;
continue;
}
}
return 0;
}
int testNum(char pswd[])
{
int count;
bool upper_flag = 0, lower_flag = 0, digit_flag = 0,space_flag=1,spcchar_flag=0;
for (count = 0; count<strlen(pswd); count++) //don't need to Size use strlen
{
if (isupper(pswd[count]))
upper_flag = 1;
else if (islower(pswd[count]))
lower_flag = 1;
else if (isdigit(pswd[count]))
digit_flag = 1;
else if (isspace(pswd[count]))
space_flag=0;
else
spcchar_flag=1;
}
if(!upper_flag)
{
cout << "The password does not contain an uppercase letter.\n";
}
if(!lower_flag)
{
cout << "The password does not contain a lowercase letter.\n";
}
if(!digit_flag)
{
cout << "The password does not contain a digit.\n";
}
if(!space_flag)
{
cout << "The password does contain a space.\n";
}
if(!spcchar_flag)
{
cout << "The password does not contain a special character(like @,%,$,# etc).\n";
}
if(upper_flag && lower_flag && digit_flag && space_flag && spcchar_flag)
return 1; //if all pass
else
return 0;
}
OUTPUT:-
Please enter a password with at least 6 characters.
Enter your password: sdas@1997
The password does not contain an uppercase letter.
Re-enter your password: sdas 1@997
The password does not contain an uppercase letter.
The password does contain a space.
Re-enter your password: Sdas@1997
The entered password is correect pattern.