In: Computer Science
Write a program that validates passwords based on the following
requirements:
• must be at least 8 characters in length
• must include at least 1 alphabet character
• must include at least 1 number
The program should implement the password checker using a function
name validate_password,
which takes two strings as input and returns one of the
following:
• 0 if the passwords match and fulfill all requirements
• 1 if the passwords are not the same length
• 2 if the a number is not found
• 3 if an alphabet character is not found
In main, test your function validate_password by creating 4 sets of
passwords and which match one of the conditions listed above and
printing the result.
In C program
#include <stdio.h>
#include <string.h>
int validate_password(char *str){
int letters=0,digits=0;;
if(strlen(str)<8){
return 1;
}
for(int i=0;i<str[i]!='\0';i++){
if( (str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z') )
letters=1;
if(str[i]>='0' && str[i]<='9')
digits=1;
}
if(!digits)
return 2;
if(!letters)
return 3;
return 0;
}
void printRes(int n){
switch(n){
case 0:printf("Valid Password\n");break;
case 1:printf("Password length is less than 8 chars\n");break;
case 2:printf("Password does not contain digits\n");break;
case 3:printf("Password does not contain characters\n");break;
}
}
int main()
{
char str[20];
printf("Enter Password : ");
scanf("%s",str);
printRes(validate_password(str));
printf("Enter Password : ");
scanf("%s",str);
printRes(validate_password(str));
printf("Enter Password : ");
scanf("%s",str);
printRes(validate_password(str));
printf("Enter Password : ");
scanf("%s",str);
printRes(validate_password(str));
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me