密码检测

题目描述

编写一个密码检测程序,程序执行时,提示"Input password:"要求用户输入密码(标准密码预先设定为"hnkd2012"),然后通过字符串比较函数比较输入密码和标准密码是否相等。若相等,则显示“Congratulation!”;若不相等,则提示"Error,please input again:"重新输入,3次都不相等则提示"The program is terminated!"并终止程序的执行。要求自己编写一个字符串比较函数,而不使用系统的strcmp( )函数。

程序的一次运行情况如下(红色部分为键盘输入数据):
Input password:hnkd
Error,please input again:2012
Error,please input again:hnkd2012
Congratulation!

输入

如题

输出

如题

#include<stdio.h>
#include<string.h> 
int main(void)
{
    int i,t,flag;//falg用来储存input password中对的数字个数
    char password[]={"hnkd2012"};
    char input[8];
    printf("Input password:");
    for(t=1;t<4;t++){
	flag=0;//注意flag要重置为0!!!!
    i=0;
    while((input[i]=getchar())!='\n'){
    	i++;
	}
	input[i]='\0';
    for(i=0;i<8;i++){
    	
    	if(input[i]!=password[i]){
    		flag++;
		}
	
	}
	if(flag==0) 
	{
	printf("Congratulation!");
	break;}//注意要使用break语句跳出if语句!!!!
	else if(t!=3) printf("Error,please input again:");
	
}
if(t==4){printf("The program is terminated!");
}
    return 0;
	}
 
   
	

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值