c语言设计密码检测程序?
发布网友
发布时间:2022-05-07 18:44
我来回答
共1个回答
热心网友
时间:2022-07-01 02:56
#include <stdio.h>
#define UC (1U<<1) // upper case
#define LC (1U<<2) // lower case
#define NUM (1U<<3) // 0-9
#define ALL (UC|LC|NUM)
int check(const char pass1[], const char pass2[])
{
const char *p = &pass1[0];
unsigned int flag = 0;
if (strlen(pass1) < 6 || strlen(pass1) > 8)
{
printf("password length is 6 to 8.\n");
return 1;
}
if (strcmp(pass1, pass2))
{
printf("the tow passwords are diffrence.\n");
return 2;
}
while (*p)
{
if (*p >= 'a' && *p <= 'z') flag |= LC;
else if (*p >= 'A' && *p <= 'Z') flag |= UC;
else if (*p >= '0' && *p <= '9') flag |= NUM;
else
{
printf("in valid charactor: %c.\n", *p);
return 3;
}
++p;
}
if (flag == ALL) return 0;
if ((flag & UC) == 0)
{
printf("lack of uppercase.\n");
}
if ((flag & LC) == 0)
{
printf("lack of lowercase.\n");
}
if ((flag & NUM) == 0)
{
printf("lack of number.\n");
}
return -1;
}
int main(int argc, char *argv[])
{
char pass1[100];
char pass2[100];
do {
printf("input password:");
scanf("%s", pass1);
printf("repeat password:");
scanf("%s", pass2);
} while (check(pass1, pass2) != 0);
return 0;
}追问为什么输入Ab1234然后就直接完了呀
追答密码正确时忘记输出提示信息了,修改一下main函数,修改如下(其实就是增加了一然 printf("password is ok: %s\n", pass1);):
int main(int argc, char *argv[])
{
char pass1[100];
char pass2[100];
do
{
printf("input password:");
scanf("%s", pass1);
printf("repeat password:");
scanf("%s", pass2);
}
while (check(pass1, pass2) != 0);
printf("password is ok: %s\n", pass1);
return 0;
}