密碼的輸入、確認和判斷比較
阿新 • • 發佈:2018-12-29
基礎版:利用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;
}