描述
小明同学最近开发了一个网站,在用户注册账户的时候,需要设置账户的密码,为了加强账户的安全性,小明对密码强度有一定要求:
1. 密码只能由大写字母,小写字母,数字构成;
2. 密码不能以数字开头;
3. 密码中至少出现大写字母,小写字母和数字这三种字符类型中的两种;
4. 密码长度至少为8
现在小明受到了n个密码,他想请你写程序判断这些密码中哪些是合适的,哪些是不合法的。
输入描述
输入一个数n,接下来有n(n≤100)行,每行一个字符串,表示一个密码,输入保证字符串中只出现大写字母,小写字母和数字,字符串长度不超过100。
输出描述
输入n行,如果密码合法,输出YES,不合法输出NO。
示例
输入:1
CdKfIfsiBgohWsydFYlMVRrGUpMALbmygeXdNpTmWkfyiZIKPtiflcgppuR
输出:YES
分析:先对密码第一个字符和密码长度进行判断,若不合法,直接判断下一个密码,再对密码这个字符串进行遍历,分别求各种字符的个数,如果输入的字符不在字母大小写和数字范围内,则不合法,直接判断下一个密码,最后判断小写字母、大写字母和数字三类中有几类。
C语言
#include <stdio.h>
#include<string.h>
int main() {
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
char str[101]={0};
scanf("%s",str);
if(strlen(str)<8)
{
printf("NO\n");
continue;
}
if(str[0]>='0'&&str[0]<='9')
{
printf("NO\n");
continue;
}
//分别记录小写字母、大写字母、数字和其他字符的个数
int character=0,CHAR=0,num=0,other=0;
for(int j=0;str[j]!='\0';j++)
{
if(str[j]>='a'&&str[j]<='z') character++;
else if(str[j]>='A'&&str[j]<='Z') CHAR++;
else if(str[j]>='0'&&str[j]<='9') num++;
else other++;
}
if(other!=0)
{
printf("NO\n");
continue;
}
//三种字符出现少于两种
if((character>0)+(CHAR>0)+(num>0)<2)
{
printf("NO\n");
continue;
}
printf("YES\n");
}
return 0;
}
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
in.nextLine();
for(int i=1;i<=n;i++)
{
String str=in.nextLine();
char[] ch=str.toCharArray();
if(ch.length<8)
{
System.out.println("NO");
continue;
}
if(ch[0]>='0'&&ch[0]<='9')
{
System.out.println("NO");
continue;
}
//分别记录小写字母、大写字母、数字和其他字符的个数
int character=0,CHAR=0,num=0,other=0;
for(int j=0;j<ch.length;j++)
{
if(ch[j]>='a'&&ch[j]<='z') character++;
else if(ch[j]>='A'&&ch[j]<='Z') CHAR++;
else if(ch[j]>='0'&&ch[j]<='9') num++;
else other++;
}
if(other!=0)
{
System.out.println("NO");
continue;
}
//字符类型个数
int count=0;
if(character>0){
count++;
}
if(CHAR>0){
count++;
}
if(num>0){
count++;
}
if(count<2){
System.out.println("NO");
continue;
}
System.out.println("YES");
}
}
}