版权声明:由陈庚汝编辑 https://blog.csdn.net/ChenGengru/article/details/84647477
基础版:利用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;
}