Reputation: 37
Im writing code that is supposed to check if a password has 1 upper case letter, 1 lower case letter and 1 digit, But did something wrong and don't know how to fix it. Can someone help?
Here is my code:
int main(int argc, char** argv) {
char password[30];
int len;
int i;
int num_alpha_num = 0;
len = strlen(password);
printf("Please enter Password: ");
scanf("%s",password);
for (i = 0; i <= len - 1; i++)
{
if (isalnum (password[i]))
{
num_alpha_num ++;
}
}
if(num_alpha_num < 1)
{
printf("Wrong");
}
else
{
printf("Done");
}
EDIT: So i fixed my code by doing this:
int main(int argc, char** argv) {
char password[30];
int len;
int i;
int num_alpha_num = 0;
printf("Please enter Password: ");
scanf("%s",password);
len = strlen(password);
for (i = 0; i <= len - 1; i++)
{
if (isalnum (password[i]))
{
num_alpha_num ++;
}
}
if(num_alpha_num < 1)
{
printf("Wrong");
}
else
{
printf("Done");
}
But it doesn't give the results im after. The program as it it is now gives me "Done" if the passwaord as 1 upper case OR 1 lower case OR 1 digit. But i want it to check for all of them, for example: the password is wrong when it is "SS2" and right when it is "Ss2". How would i do this?
Final Code (Thanks Everyone!) :
int main(int argc, char** argv) {
char password[30];
int len;
int i;
int num_upper_num = 0;
int num_lower_num = 0;
int num_digit_num = 0;
printf("Please enter Password: ");
scanf("%s",password);
len = strlen(password);
for (i = 0; i <= len - 1; i++)
{
if (isupper (password[i]))
{
num_upper_num = 1;
}
if (islower (password[i]))
{
num_lower_num = 1;
}
if (isdigit (password[i]))
{
num_digit_num = 1;
}
}
if(num_upper_num + num_lower_num + num_digit_num == 3)
{
printf("This password is valid");
}
else
{
printf("This password is invalid");
printf("\nA password must contain 1 upper case, 1 lower case, and 1 digit.");
}
return (EXIT_SUCCESS);
}
Upvotes: 0
Views: 318
Reputation: 4207
There are a few things wrong with this program. let's check them line by line.
char password[30];
When taking input, a large buffer size is generally preferred. Otherwise if the input is too long for the array, your scanf
will cause a buffer overflow, characters will be written outside the array (more on scanf
soon). I'd change this buffer size to 1024 but I've even seen people who make it 8192.
int num_alpha_num = 0;
is bad for checking your specific requirement, but that comes later.
len = strlen(password);
password
is currently uninitialized, so it will hold garbage values, god knows what the length could become. Even if you zero-initialize the buffer, len
will be 0, because there will be a NUL terminator right at the beginning. This should come after input (though you have fixed this in the edit).
scanf("%s",password);
See A beginner's guide away from scanf
. In this specific case, it can cause a buffer overflow, as I previously mentioned. A better alternative is using fgets
where you specify the max length as a parameter. See the man page on fgets
and a few other input functions.
Now, coming to the current issue you are having:
for (i = 0; i <= len - 1; i++)
{
if (isalnum (password[i]))
{
num_alpha_num ++;
}
}
if(num_alpha_num < 1)
{
printf("Wrong");
}
else
{
printf("Done");
}
This uses isalnum
, which can only check if a number is an alphanumeric character. Not what you want. To meet your requirements, one solution would be to create 3 bool
/_Bool
variables called has_upper
, has_lower
, has_num
and set them all to false before you enter the loop. While in the loop, check for each possible case with isupper
, islower
and isdigit
functions defined in <ctype.h>
and if one of the conditions returns true, set the respective bool
variable to true
. At the end of the loop, check if all 3 variables are true for a correct password.
Here's an example program that demonstrates this:
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
int main(int argc, char** argv)
{
char password[1024]; // Increased buffer size
int len;
int i;
bool has_upper = false, has_lower = false, has_num = false; // To check each requirement
printf("Please enter Password: ");
fgets(password, sizeof password, stdin); // Safer than scanf
len = strlen(password); // Take length after input
for (i = 0; i < len; i++) // i <= len - 1 is a bit redundant, i < len is better
{
if (isupper(password[i]))
has_upper = true;
else if (islower(password[i]))
has_lower = true;
else if (isdigit(password[i]))
has_num = true;
}
if(has_upper && has_lower && has_num)
printf("Done\n");
else
printf("Wrong\n");
}
Upvotes: 1
Reputation: 401
See If this works:-
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char ** argv)
{
char password[30];
int len;
int i;
int flagA = 0;
int flaga = 0;
int flag1 = 0;
printf("Please enter Password: ");
scanf("%s", password);
len = strlen(password);
for (i = 0; i <= len - 1; i++)
{
if ((int) password[i] > 97 && (int) password[i] < 122 )
{
flaga = 1;
}
if ((int) password[i] > 65 && (int) password[i] < 90 )
{
flagA = 1;
}
if ((int) password[i] > 48 && (int) password[i] < 57 )
{
flag1 = 1;
}
}
if ((flaga+flagA+flag1) == 3)
{
printf("Done");
}
else
{
printf("Wrong");
}
return 0;
}
Upvotes: 0