题目描述:
很多网站初始密码都是“123456”,为保障账号安全,第一次登录会强制修改密码为加强密码,已知某网站加强密码需要同时满足以下规定:长度为6-16;必须同时包含大写字母、小写字符、数字和特殊符号;首位必须为大写字母。
请你编写一个程序,判断用户设置的密码是否为加强密码。
输入格式:
输入第一行为一个整数T(1<=T<=20),代表测试数据的组数,随后T行,每行都是由小写字母、大写字母、数字、特殊符号组成的一串字符s,长度不超过20,特殊符号有且仅有@、#、$、%、^、&、*。
输出格式:
输出每行字符串是否为加强密码,是,输出“Yes”,不是,输出“No”,每组输出占一行。
输入样例:
4
Ab12~
Hello123@
abc#123
good$666
输出样例:
No
Yes
No
No
参考答案如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void judge(char* ch)
{
int len = strlen(ch);
int hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;
if (len < 6 || len > 16)
{
printf("No\n");
return;
}
if (isupper(ch[0]))
{
hasUpper = 1;
}
for (int i = 0; i < len; i++)
{
if (islower(ch[i]))
{
hasLower = 1;
} else if (isdigit(ch[i]))
{
hasDigit = 1;
}
else if (ch[i] == '@' || ch[i] == '#' || ch[i] == '$' || ch[i] == '%' || ch[i] == '^' || ch[i] == '&' || ch[i] == '*')
{
hasSpecial = 1;
}
}
if (hasUpper && hasLower && hasDigit && hasSpecial)
{
printf("Yes\n");
}
else
{
printf("No\n");
}
}
int main()
{
int T;
scanf("%d", &T);
getchar(); // Consume the newline character ,it's important.
for (int i = 0; i < T; i++)
{
char ch[21]; // Increased the size to 21 to include the null terminator
fgets(ch, sizeof(ch), stdin);
judge(ch);
}
return 0;
}
NO.30
<C语言数据结构>