C语言 密码检测

题目描述

编写一个密码检测程序,程序执行时,提示"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!

输入

如题

输出

如题

提示

本题由实验9实验范例1稍作改编而成。

难度系数为4

代码

#include <stdio.h>  
#include<string.h>

int judge(char *s)
{
	int f=1;
	int i;
	char b[]={'h','n','k','d','2','0','1','2'};
	for(i=0;i<8;i++)
	{
		if(s[i]!=b[i])	f=0;
	}
	return f;
}

int main()
{	
	char s[1000];
	int i,p=0;
	
	printf("Input password:");
	
	for(i=1;i<3;i++)
	{	
		getchar();
		gets(s);
		if(judge(s)==1)
		{
			p=1;
			break;
		}
		else 
			printf("Error,please input again:");
	}
	gets(s);
	if(judge(s)==1)
		printf("Congratulation!");
	else 
		printf("The program is terminated!");
	return 0;
}
发布了47 篇原创文章 · 获赞 29 · 访问量 1489

猜你喜欢

转载自blog.csdn.net/Qianzshuo/article/details/103758479