【发布时间】:2014-04-26 07:38:45
【问题描述】:
此代码在两件事上出现故障,我无法诊断,但与 if 语句隔离:if (strlen(strA) > 8)。假设我输入字符串“1234567*aA”,程序告诉我密码太长,所以我输入字符串“12345”,然后触发else if语句:else if (strlen(strA)
我注意到的第二个问题是,如果我输入一个字符串“1111111111111111111”,这显然太长了,程序说密码太长,但整个 do/while 循环终止并要求我确认我的密码.它应该要求我再次输入密码。
如果我取出 if 语句:if (strlen(strA) > 8) 和 else if (strlen(strA)
谁能诊断这个问题?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char strA[10];
char strB[10];
char strC[] = {'1','2','3','4','5','6','7','8','9','0'};
char strD[] = {'!','@','#','$','%','^','&','*','(',')'};
char strE[] = {'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z'};
char strF[] = {'A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S',
'T','U','V','W','X','Y','Z'};
int i, j, k;
do {
k = 0;
j = 0;
printf("Please enter your password: ");
scanf("%s", &strA);
printf("%s\n", strA);
if(strlen(strA) > 8) {
printf("That password is too long\n");
}
else if(strlen(strA) < 9) {
while (j <= 9) {
for(i = 0; i <= 9; i++) {
if(strA[j] == strC[i]) {
printf("there is a number in this string.\n");
k++;
j = 0;
while (j <= 9) {
for(i = 0; i <= 9; i++) {
if(strA[j] == strD[i]) {
printf("there is a character in this string.\n");
k++;
j = 0;
while(j <= 9) {
for(i = 0; i <= 25; i++) {
if(strA[j] == strE[i]) {
printf("there is a lowercase letter in this string.\n");
k++;
j = 0;
while(j <= 9) {
for(i=0;i<=25;i++) {
if(strA[j] == strF[i]) {
printf("there is an uppercase letter in this string.\n");
k++;
}
}
j++;
}
}
}
j++;
}
}
}
j++;
}
}
}
j++;
}
if(k < 4) {
printf("Your password must contain at least one uppercase letter, one lowercase letter, a number, and a special character.\n");
}
}
} while(k < 4);
printf("Please confirm your password: ");
scanf("%s",&strB);
while(strcmp(strA, strB) != 0) {
printf("%s\n",strB);
printf("Your passwords do not match.\nPlease confirm your password: ");
scanf("%s",&strB);
}
putchar('\n');
printf("%s\n", strA);
printf("%s\n", strB);
return 0;
}
【问题讨论】:
-
筑巢的圣母! o.O
-
代码本身看起来是加密的:D
-
它只有自己的系统... ;-) @Yelinna
-
如果密码长度太大,为什么不打破while循环?它将保存一个“其他”
-
哈哈哈。我是个菜鸟,所以这里没有约定 ;)
标签: c string loops if-statement