我的程序,用于生成可接受的密码,除了输入提示之外不显示任何输出,它允许我输入密码但程序立即结束。我的调试器上没有错误或警告。想知道是否有人可以给我一些意见。
#include "stdafx.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char password[15] = {'\0'};
char search[15] = { '\0' };
int a, b, c, d, e;
char punct[] = { '!','@','#','$','%','^','&','*','?','_', '\0' };
printf("Enter your test password: \n");
fgets(password, 15, stdin );
a = strnlen(password, 15);//judges length of search between the numbers 2 and 15
b = strncmp(password, punct, 10);
c = isalpha(strnlen(password,15));
d = isdigit(strnlen(password, 15));
e = a + b + c + d;
if (a < 2 || a>15) {
printf("Must have exactly 2-15 characters.\n");
if (strncmp(password, punct, 10) == false) {//must have one of the characters included in password
printf("Must have character punctutation\n");
}
if (isdigit(strnlen(password, 15)) == false) {
printf("Must consist of at least one number 0-9.\n");
}
if (isalpha(strnlen(password, 15)) == false) {
printf("Must consist of at least one letter a-z\n");
}
else {
printf("You have inputted a legal password!\n");
}
}
return 0;
}
答案 0 :(得分:0)
由于strnlen(password, 15)
介于2
和15
之间,您未获得输出,请使用else
部分:
if (a < 2 || a > 15) {
...
} else {
/* your output here */
}
另一方面:
if (isdigit(strnlen(password, 15)) == false) {
是一个废话,isdigit
检查字符是否为十进制数字,但是您传递size_t
而不是char
,isalpha
相同。