c - Why is my password checking code not working correctly? It gives same output regardless of whatsoever input -
i made password checking program checks following criteria:-
should have atleast
- 1 uppercase
- 1 lower case
- 1 special character
- 1 number
- should lesser 100 characters
and thats it. have not given lower limit. , no matter input give (correct or incorrect), program gives me same or similar output attached in screenshot.
for eg:- pratik10, pratik10, pratikten, pr@tik10, same output "password fine , valid".
why program not checking defined conditions correctly? not printing counters of password correctly.
following code:
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <string.h> int main() { char x[100]; int i; int uc=0; int lc=0; int num=0; int misc=0; printf("enter password\n"); scanf("%s",x); for(i=0;i<100;i++) { if (isalpha(x[i])) { if (isupper(x[i])) { uc++; } if (islower(x[i])) { lc++; } } if (isdigit(x[i])) { num++; } else { misc++; } } printf("checking password\n"); printf("%d uc\n",uc); printf("%d lc\n",lc); printf("%d num\n",num); printf("%d misc\n",misc); if ((uc > 0) && (lc > 0) && (num > 0) && (misc > 0)) { printf("password fine , valid\n"); } else { if(lc<=0) { printf("lowercase character(s) missing cannot proceed without inclusion\n"); } if(uc<=0) { printf("uppercase character(s) missing cannot proceed without inclusion\n"); } if(num<=0) { printf("number(s) missing cannot proceed without inclusion\n"); } if(misc<=0) { printf("special character(s) missing cannot proceed without inclusion\n"); } printf("please include missing parameters in combination validate password , try again\n\n"); } return 0; } how correct this?
output:

you should check null-terminated string provided input user.
in other words, should iterate x until encounter null-character.
change this:
for (i = 0; < 100; i++) to this:
for (i = 0; x[i] != 0; i++) a second problem not using if/else properly.
as result, every character not digit counted misc.
change this:
if (isdigit(x[i])) to this:
else if (isdigit(x[i]))
Comments
Post a Comment