我正在尝试根据一组规则制作一个程序来确定密码的有效性。
这是我所拥有的:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
int main()
{
int uppercase = 0;
int length = 0;
int numbers = 0;
int others = 0;
char password[13];
char yesOrNo;
printf("Your password must be 8-12 characters long.\n"
"It must contain at least one symbol, uppercase letter, and number.\n\n");
COMEBACK:
printf("Please enter your password:");
scanf(" %s", &password);
while (password != 'NULL') { // Tried 0 here, tried '\0', but to no avail.
if (isalpha(password)) {
length += 1;
if (isupper(password)) {
uppercase += 1;
}
}
else if (isdigit(password)) {
numbers += 1;
length += 1;
}
else {
length += 1;
}
// This is just a test, to see if it was working.
printf("%d - %d - %d - %d --- %s",
uppercase, length, numbers, others, password);
}
if ((uppercase > 0) && (numbers > 0)
&& (length >= 8) && (length <= 12) && (others > 0)) {
printf("Good job, you've done your password correctly.");
} else {
printf("%d - %d - %d - %d --- %s \t Incorrect..",
uppercase, length, numbers, others, password); // Same thing here.
scanf("%s", &yesOrNo);
switch (yesOrNo) {
case 'y':
goto COMEBACK;
break;
case 'n':
printf("Sorry you're dumb man..");
break;
default:
printf("Please enter a valid password.");
}
}
return 0;
}
我遇到的问题是,while 循环永远不会结束,因为它似乎无法为我的密码数组找到终止符。我输入了“\0”以及“0”。但我还是想不通。任何帮助表示赞赏。谢谢。