题目:Linux操作系统中,用户更换密码时,首先要输入原密码,然后输入新密码。新密码可用的条件是跟原密码相比,至少有三个字符不一样。
若密码符合条件,则重新输入新密码,请输出"Valid password, please input your password again:\n”;若密码输入不符合要求,请输出"Invalid password, please input another password'\n",直到符合条件为止。当第一次输入的密码与第一次的不相符时,输出"Different from your previous one, please input again:\n"
请你编写一个C语言程序,模拟这个过程。
提醒:使用字符串。
输入提醒"Input your old password:\n","Input a new password:\n","You've changed your password successfully!"
涉及知识点:字符串,循环控制结构,选择控制结构
初学者代码示例:
#include <stdio.h>
#include <string.h>
#define N 100
int main()
{
int i,count=0;
char s1[N],s2[N],s3[N];
printf("Input your old password:\n");
fgets(s1,N-1,stdin);
printf("Input a new password:\n");
fgets(s2,N-1,stdin);
for (i=0;(i<N)&&(s1[i]!='\0')||(s2[i]!='\0');i++)
{
if (s1[i]==s2[i]) count++;
}
if (count>=3)
{
while (1)
{
count=0;
printf("Invalid password, please input another password!\n");
fgets(s2,N-1,stdin);
for (i=0;(i<N)&&(s1[i]!='\0')||(s2[i]!='\0');i++)
{
if (s1[i]==s2[i]) count++;
}
if (count>=3) continue;
else break;
}
}
printf("Valid password, please input your password again:\n");
fgets(s3,N-1,stdin);
if (strcmp(s3,s2)!=0)
{
while (1)
{
printf("Different from your previous one, please input again:\n");
fgets(s3,N-1,stdin);
if (strcmp(s3,s2)!=0) continue;
else break;
}
}
printf("You've changed your password successfully!");
return 0;
}
代码已经修正过几遍了,各位大佬看到以后有什么想法都可以提出来嘿嘿嘿!
这道题也上传到哈工大编程平台习题市场了~