密码的输入、确认和判断比较

基础版:利用strcmp函数进行字符串的比较

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/* 简单密码比较 */
/* written by Chen Gengru */
/* updated on 2018-11-30 */
int main()
{
	char password1[20], password2[20];
	
	int i;
	
	printf("Please input password\n");
	scanf("%s", &password1);
	
	printf("check your password\n");
	scanf("%s", &password2);
	
	if (strcmp(password1, password2) == 0)
	{
		printf("correct!\n");
	}
	else
	{
		printf("wrong!\n");
	}
	
	return 0;
 } 

进阶版:加入重新输入密码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/* 简单密码比较 */
/* written by Chen Gengru */
/* updated on 2018-11-30 */
int main()
{
	char password1[20], password2[20];
	
	int iWrongcheck;
	
	retype: printf("Please input password\n");
	        scanf("%s", &password1);
	
	printf("check your password\n");
	scanf("%s", &password2);
	
		if (strcmp(password1, password2) == 0)
	    {
	       printf("correct!\n");
	    }
	    else
    	{
	    	printf("wrong! Press 1 to retype password. Press 0 to end.\n");
	    	scanf("%d", &iWrongcheck);
	    	if (iWrongcheck)
	    	{
	    		goto retype;
			}
			else
			{
				return 0;
			}
     	}
	
	return 0;
 } 

高级版:在密码输入和确认的基础上,判断密码,若重复输错三次,结束

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* 密码比较(确认加判断) */
/* written by Chen Gengru */
/* updated on 2018-12-6 */ 
int main()
{
	char password1[20], password2[20], password3[20];
	
	int i, iWrongcheck;
	
	retype: printf("Please define password\n");
	        scanf("%s", &password1);
	
	printf("check your password\n");
	scanf("%s", &password2);
	
		if (strcmp(password1, password2) == 0)
	    {
	       printf("correct!\n");
	    }
	    else
    	{
	    	printf("wrong! Press 1 to retype password. Press 0 to end.\n");
	    	scanf("%d", &iWrongcheck);
	    	if (iWrongcheck)
	    	{
	    		goto retype;
			}
			else
			{
				return 0;
			}
     	}
     	
     printf("Please input your password.\n");
	 scanf("%s", &password3);
	
	for (i == 0; i < 3; i++)
	{
		if (strcmp(password1, password3) == 0)
		{
			printf("Correct! Welcome!\n");
			return 0;
		}
		else
		{
			printf("Wrong password! Please input again!\n");
			scanf("%s", &password3);
		}
	}
	
	printf("You have input the password wrongly for three times! END\n");
	
	return 0;
 } 
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值