I am having trouble with this project I'm doing.
Here is the 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. Use the password 'wewew43x' as a test.
Here is my failing code:
Code:
int main()
{
char pw[20],p;
int i=0;
printf("Enter the password:");
while((p=getch())!= 13)
{
pw[i++] = p;
printf("*");
}
pw[i] = '\0';
printf("\nYou have entered: %s",pw);
if (i <8 )/*If the password has less than 8 characters it is INVALID!*/
{
printf("\nInvalid password. Passwords must have at least 8 characters.\n");
}
if (!( isalnum(pw[i])) )/* if pw contains special chars it is INVALID*/
{
printf("\nInvalid password. Passwords cannot contain special characters.\n");
}
if ( isdigit(pw[i]) < 2 ) /*if pw contains less than 2 digits it is INVALID*/
{
printf("\nInvalid password. Passwords must contain at least 2 digits.\n");
}
else
printf("\nValid password. All conditions have been met.\n");
return 0;
}
My problem is that I'm not thoroughly understanding how to get the functions isalnum & isdigit to work with my code. When I type the password that I want to test with, it just says its invalid, when it should be valid. What am I doing wrong?