C programming Question on Characters
-
/*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }
-
/*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }
I see several things wrong with your approach: 1) The first time through the loop,
tuna
is uninitialized so theprintf()
statement is going to print an incorrect character, 2) Thescanf()
statement is expecting to receive achar
(%c
) but you are giving it the address of anint
, 3) Theisupper()
,isalpha()
, andisdigit()
functions return a non-zero value if the parameter is an uppercase alphabetic letter, and zero otherwise. You are then comparing this totuna
. While syntactically correct, it is nonsensical, 4) If your password is to hold numbers, letters, and symbols, the password type should not be anint
, but should probably be astring
. Consider something like:int reqd = 0;
char pwd[32];
printf("Enter a password: ");
scanf("%s", pwd);
for (int x = 0; x < strlen(pwd); x++)
{
if (isupper(pwd[x])) // this does not check for special characters
reqd |= 0x1;
else if (isalpha(pwd[x]))
reqd |= 0x2;
else if (isdigit(pwd[x]))
reqd |= 0x4;
}if (reqd == 0x07)
printf("Requirements have been met.\n");
else
printf("Requirements have NOT been met.\n");"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I see several things wrong with your approach: 1) The first time through the loop,
tuna
is uninitialized so theprintf()
statement is going to print an incorrect character, 2) Thescanf()
statement is expecting to receive achar
(%c
) but you are giving it the address of anint
, 3) Theisupper()
,isalpha()
, andisdigit()
functions return a non-zero value if the parameter is an uppercase alphabetic letter, and zero otherwise. You are then comparing this totuna
. While syntactically correct, it is nonsensical, 4) If your password is to hold numbers, letters, and symbols, the password type should not be anint
, but should probably be astring
. Consider something like:int reqd = 0;
char pwd[32];
printf("Enter a password: ");
scanf("%s", pwd);
for (int x = 0; x < strlen(pwd); x++)
{
if (isupper(pwd[x])) // this does not check for special characters
reqd |= 0x1;
else if (isalpha(pwd[x]))
reqd |= 0x2;
else if (isdigit(pwd[x]))
reqd |= 0x4;
}if (reqd == 0x07)
printf("Requirements have been met.\n");
else
printf("Requirements have NOT been met.\n");"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
I didn't get the logic behind "reqd" in the if else part. Can you please explain?
-
/*Write a programme to check a password if it has an uppercase letter, a digit and a special symbol. (I have provided what I have done and I am not getting the output as required. Need explanation on where I messed it up.)*/ #include #include #include int main() { int i; int tuna; do{ printf("Enter a password:%c \n", tuna); scanf(" %c", &tuna); } while( tuna != (isupper(tuna), isalpha(tuna), isdigit(tuna)) ); return 0; }
-
I didn't get the logic behind "reqd" in the if else part. Can you please explain?
-
I presume this is for a class and I suspect you have not been taught bit manipulation. Unless you learn about it yourself, you probably do not want to hand in a solution that uses it.
jschell wrote:
Unless you learn about it yourself, you probably do not want to hand in a solution that uses it.
Good point. I didn't think through my offering well enough.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
I didn't get the logic behind "reqd" in the if else part. Can you please explain?
You could (possibly) simplify to something like:
int upper = 0;
int alpha = 0;
int digit = 0;...
if (isupper(pwd[x])) // this does not check for special characters
upper = 1;
else if (isalpha(pwd[x]))
alpha = 1;
else if (isdigit(pwd[x]))
digit = 1;...
if (upper == 1 && alpha == 1 && digit == 1)
printf("Requirements have been met.\n");
else
printf("Requirements have NOT been met.\n");"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles