本题要求你帮助某网站的用户注册模块写一个密码合法性检查的小功能。该网站要求用户设置的密码必须由不少于6个字符组成,并且只能有英文字母、数字和小数点 .,还必须既有字母也有数字。
输入格式:
输入第一行给出一个正整数 N(≤ 100),随后 N 行,每行给出一个用户设置的密码,为不超过 80 个字符的非空字符串,以回车结束。
输出格式:
对每个用户的密码,在一行中输出系统反馈信息,分以下5种:
输入样例:
5 123s zheshi.wodepw 1234.5678 WanMei23333 pass*word.6
输出样例:
Your password is tai duan le. Your password needs shu zi. Your password needs zi mu. Your password is wan mei. Your password is tai luan le.
分析:
1、计算输入字符串的长度,判断是否满足条件(>=6) 2、对于长度满足条件的字符串
char str[101][88];
int fun1(int i,int len)
{
int j;
int l=1;
for(j=0;j<len;j++)
{
if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
else
{
l=0;
break;
}
}
return l;
}
int fun2(int i,int len)
{
int j;
int temp1=0;
int temp2=0;
for(j=0;j<len;j++)
{
if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
}
if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
if(temp1==0&&temp2>0) return 0; //只有字母,没有数字
if(temp1>0&&temp2>0) return 1; //既有数字,又有字母
}
3、输出
代码:
#include<stdio.h>
char str[101][88];
int fun1(int i,int len)
{
int j;
int l=1;
for(j=0;j<len;j++)
{
if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')
||(str[i][j]>='0'&&str[i][j]<='9')||(str[i][j]=='.')) ;
else
{
l=0;
break;
}
}
return l;
}
int fun2(int i,int len)
{
int j;
int temp1=0;
int temp2=0;
for(j=0;j<len;j++)
{
if(str[i][j]>='0'&&str[i][j]<='9') temp1++;
if((str[i][j]>='a'&&str[i][j]<='z')||(str[i][j]>='A'&&str[i][j]<='Z')) temp2++;
}
if(temp1>0&&temp2==0) return -1; //只有数字,没有字母
if(temp1==0&&temp2>0) return 0; //只有字母,没有数字
if(temp1>0&&temp2>0) return 1; //既有数字,又有字母
}
int main()
{
int i;
int N;
scanf("%d",&N);
getchar(); //吸收回车
for(i=0;i<N;i++)
gets(str[i]);
for(i=0;i<N;i++)
{
int len=strlen(str[i]);
// printf("len==%d ",len);
if(len>=6)
{
int temp1=fun1(i,len);
if(temp1==1)
{
int temp2=fun2(i,len);
if(temp2==-1) printf("Your password needs zi mu.\n");
if(temp2==0) printf("Your password needs shu zi.\n");
if(temp2==1) printf("Your password is wan mei.\n");
}
else printf("Your password is tai luan le.\n");
}
else
{
printf("Your password is tai duan le.\n");
}
}
return 0;
}