#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
}
#include<stdio.h>#include<string.h>
void main()
{
char str1[20],str2[20]="abcd"//定义两个字符串数组,str2存放已知密码,str1存放输入密码
int i=0,k,flag=0
printf("请输入密码:")
while(1)//死循环
{
gets(str1)//输入密码
k=strcmp(str1,str2)//比较,输入密码和已知密码相等,k=0
++flag//计数器加1
if(!k)
{ printf("欢迎使用\n")
break
}
else if(k&&flag!=3) printf("密码输入错误,请再次输入:\n")
if(flag==3)//当输入超过三次,跳出循环,结束程序
{
printf("对不起,您无权使用")
break
}
}
}
代码如下:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
int main()
{
int len = 0
char password[10] = {0}
printf("请输入密码:")
scanf("%s", password)
len = strlen(password)
if (len < 5) {
printf("密码长度不足!\n")
}
else if (len > 5) {
printf("密码长度过长!\n")
}
else {
// 默认密码是 admin
if (strcmp(password, "admin") == 0) {
printf("登录成功!\n")
}
else {
printf("密码不正确!\n")
}
}
system("pause")
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)