I don't get very well why you are using an array of 15-chars long passwords, but I suppose your criteria refers to just one of those password and not to the others: you want to check that a password has requirements to be considered a "good" password; this is my understanding. Then...
The function gets
is rather unsafe. Avoid using it.
The idea is to ask for a password, check it and loop if it does not fit your criteria. There's not a single way to do it of course.
// include files for I/O funcs
#include <stdio.h>
for(;;)
{
printf("insert pwd: ");
gets(buffer); // argh I've said: don't use this
if ( !pass_criteria(buffer) ) {
printf("criteria are ....\n");
} else break;
}
Then pass_criteria could be something like
// include files for strlen and is* funcs
#include <ctype.h>
#include <string.h>
int pass_criteria(const char *buf)
{
int upcount, lowcount, numcount;
if (strlen(buf) < minimum_pass_len ||
strlen(buf) > max_pass_len) return 0; // 0 being false
for (int i = 0; i < strlen(buf); ++i) {
if (isdigit(buf[i]) numcount++;
if (isupper(buf[i]) upcount++;
if (islower(buf[i]) lowcount++;
}
return numcount > 0 && upcount > 0 && lowcount > 0;
}
It's easy to change criteria, e.g. if you want at least 2 number (digit), put numcount > 1
and so on.
Instead of gets
Gets is dangerous for buffer overflow. Try using e.g. fgets like this:
fgets(buffer, buffer_size, stdin);
where buffer_size
is the size of your buffer (15 in your case, but avoid using a literal constant; prefer a proper #define
or use sizeof
, e.g. sizeof (password[0])
. Note also that fgets does not discard final newline.