Imagine you are developing a software package that requires users to enter their own passwords. Read the input as a C-string or a string object. Your software requires that users’ passwords be sent to special functions that will verify if the characters used meet the following criteria:
The password should be at least six characters long.
The password should contain at least one uppercase and at least one lowercase letter.
The password should have at least one digit.
Write a program that asks for a password then verifies that it meets the stated criteria. If it doesn’t, the program should display a message telling the user why.
You may consider using various functions such as the following ...
void displayRequirements(); void displayResult(char[]); bool isValid(char[]); bool hasLength(char[]); bool hasLower(char[]); bool hasUpper(char[]); bool hasDigit(char[]);
or implement your own functional decomposition.
Sample Screen Output (user input in bold):
Password Requirements: - The password should be at least 6 characters long. - The password should contain at least one uppercase and at least one lowercase letter. - The password should have at least one digit. Enter a password: srjc The password is invalid. It should be at least 6 characters long. It should contain at least one uppercase letter. It should have at least one digit. Enter another password: srjc2020 The password is invalid. It should contain at least one uppercase letter. Enter another password: Srjc2020 The password is valid. Process returned 0 (0x0) execution time : 37.238 s Press any key to continue.
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile
and execute it.
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include<stdbool.h>
void displayRequirements() //function to display requirements
{
printf("\nPassword Requirements:");
printf("\n - The password should be at least 6 characters
long.");
printf("\n- The password should contain at least one uppercase and
at least one lowercase letter.");
printf("\n - The password should have at least one digit");
}
bool hasLower(char ch[100])
{
int count=0;
for(int i=0;i<strlen(ch);i++)
{
if(ch[i]>='a' && ch[i]<='z') //check for lower Case
Character presence
count=1;
}
if(count==1)
return true;
else
return false;
}
bool hasUpper(char ch[100])
{
int count=0;
for(int i=0;i<strlen(ch);i++)
{
if(ch[i]>='A' && ch[i]<='Z') //check for Upper Case
Character present
count=1;
}
if(count==1)
return true;
else
return false;
}
bool hasDigit(char ch[100])
{
int count=0;
for(int i=0;i<strlen(ch);i++)
{
if(ch[i]>='0' && ch[i]<='9') //check for digit
count=1;
}
if(count==1)
return true;
else
return false;
}
bool hasLength(char ch[100])
{
if(strlen(ch)>=6) //check if length is greater than 6
return true;
else
return false;
}
void displayResult(char pass[100])
{
bool invalid=true;
int error=0,k=0;
while(invalid) //while loop to run till the user enter a valid
input
{
error=0; //reset error value with every iteration
if(k>0) //check to see if user has entered password for first
time to ask enter password again
{
printf("\nEnter another password: ");
scanf("%s",pass);
}
if(!hasLength(pass)) //check password length if true below checks
are performed
{
if(error==0) // if there is no error before displaying invalid
password
{
error=1;
printf("The password is invalid.\n\n");
}
printf("It should be at least 6 characters long.\n");
}
if(!hasLower(pass)) // check to see if password has a lowercase
character
{
if(error==0) // if there is no error before displaying invalid
password
{
error=1;
printf("The password is invalid.\n\n");
}
printf("It should contain at least one lowercase letter.\n");
}
if(!hasUpper(pass)) //check to see if there is a uppercase
character
{
if(error==0)
{
error=1; // if there is no error before displaying invalid
password
printf("The password is invalid.\n\n");
}
printf("It should contain at least one uppercase letter.\n");
}
if(!hasDigit(pass)) //check to see if password has a digit
{
if(error==0)
{
error=1;
printf("The password is invalid.\n\n");
}
printf("It should have at least one digit.\n");
}
if(error==0) //if there is no error password is valid
{
printf("The password is valid.\n\n");
invalid=false;
}
k++; //k increased to check if it first input entry
}
}
int main()
{
char pass[100];
clock_t ts=clock; //start time to calculate Execution time
Taken
displayRequirements(); // display Requirement to User
printf("\n\nEnter a password: ");
scanf("%s",pass); // Taking user input
displayResult(pass); //Display Result to user
clock_t t = clock() ; //Calculating time after exection
double time_taken = ((double)ts-t)/CLOCKS_PER_SEC;
printf("\nProcess returned 0 (0x0) execution time : %f
s",time_taken);
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.