初学者的一道自创C语言习题:模拟Linux密码设置程序

题目: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;
}

代码已经修正过几遍了,各位大佬看到以后有什么想法都可以提出来嘿嘿嘿!

这道题也上传到哈工大编程平台习题市场了~

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值