我正在尝试编写一个代码,该代码使用$
函数检查密码是否包含数字,大写字母和for
符号,但是如果我将{}添加到{ {1}}功能无法正常工作。
如果我这样做,将无法正常工作:
for
for (x = 0; x <= 10; x++) {
if (isdigit(password[x])) {
number++;
}
} // why does removing the {} from for make it work
答案 0 :(得分:0)
各种问题
限制输入
使用宽度限制修饰符,并且不要传递数组的地址。传递数组(没有&
),它将转换为数组第一个元素的地址。不需要" "
开头的格式,因为"%s"
已经扔掉了可选的前导空白。
int x = 10;
char password[x];
// scanf(" %s", &password);
scanf("%9s", password);
没有什么理由会小气缓冲区大小。建议:
char password[80];
scanf("%79s", password);
迭代为空字符
// Bad! this iterates 11 times!
// for (x = 0; x <= 10; x++)
// Iterate up to the null character
for (x = 0; password[x]; x++)
高级:is...()
is...(int ch)
对于unsigned char
范围和EOF
范围内的值有效。确保不传递其他负值。
// if (isupper(password[x]))
if (isupper((unsigned char) password[x]))
也可以使用一个循环。
for (x = 0; password[x]; x++) {
if (isdigit((unsigned char) password[x])) {
number++;
} else if (isupper((unsigned char) password[x])) {
uppercase++;
} else if (password[x] == '$') {
dollar++;
}
}
高级:安全性
使用密码完成擦洗内存很好。
volatile char *p = password;
for (x = 0; x < sizeof password; x++)
p[x] = 0;
}
注意:memset(password ...)
不好,因为它可能已被优化。