C语言对于实现字符验证,没有提供真正的验证函数,那么我们可以借用 strcmp() 函数,实现字符的比较,C语言对字符的比较叫做“字典序”。
#include <stdio.h>
#include <string.h>
int main(){
int i = 0;
char pwd[10] = { 0 };
printf("请输入保险箱密码:");
while (i < 3){
scanf("%s", pwd);
if (0==strcmp(pwd, "123456")){
printf("密码正确!!!\n");
break;
}
else if (i == 2){
printf("你错过三次机会,导致你失去一个亿\n");
break;
}
else{
printf("密码错误,请重新输入:");
}
i++;
}
return 0;
}