C Project Problem Description: Some Websites impose certain rules for passwords.
ID: 3939173 • Letter: C
Question
C Project
Problem Description:
Some Websites impose certain rules for passwords. Write a program and method that checks whether a string is a valid password. Suppose the password rule is as follows:
A password must have at least eight characters.
A password consists of only letters and digits.
A password must contain at least two digits.
Write a program that prompts the user to enter a password and displays "valid password" if the rule is followed or "invalid password" otherwise.
Sample 1
Enter a string for password: wewew43x
valid password
Sample 2
Enter a string for password: 343a
invalid password
Explanation / Answer
#include<stdio.h>
#include<string.h>
int valid(char p[]){
int i,digit=0;
char ch;
size_t len = strlen(p);
if(len <8)
return 0;
for(i=0;i<len;i++)
{
ch=p[i];
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
//do nothing
}
else if(ch >= '0' && ch <= '9')
{
digit++;
}
else
{
return 0;
}
}
if(digit<2)
return 0;
return 1;
}
int main(){
char password[20];
printf("Enter password: ");
scanf("%s", password);
printf("Your password is %s.", password);
if(valid(password))
printf("Valid Password");
else
printf("Invalid Password");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.