是否可以使用'和'& '或'与isupper和isdigit我正在尝试运行下面的代码,但它不起作用。 [英] Is it okay to use 'and' & 'or' with isupper and isdigit I'm trying to run the code below but it's not working.
问题描述
int main()
{
char pass[8];
printf("Enter your password: (Maximum 8 characters)");
scanf("%c",pass);
if(isdigit(pass[0]) && isdigit(pass[1])){
printf("AKH");
}
return 0;
}
如果第一个或第二个字符是数字,我期待printf运行,但它只是适用于第一个角色。
我尝试过:
我尝试使用字符1和0进行OR而不是引用数组,它运行正常。我需要帮助
I'm expecting the printf to run if either the first or second character is a digit, but it only works for the first character.
What I have tried:
I tried doing the OR with characters '1' and '0' instead of referencing the array and it worked fine. Please I need help
推荐答案
尝试
Try
if((isdigit(pass[0]) || isdigit(pass[1])){
小心丢失'(代码中的'
&&是C表示法逻辑AND:如果两个部分都为真,则结果为真。
Be careful with missing '(' in your code
&& is C notation for logical AND: result is true if both part are true.
((isdigit(pass[0]) && isdigit(pass[1]))
// means
((isdigit(pass[0]) AND isdigit(pass[1]))
||是逻辑OR的C表示法:如果至少结果为真一部分是真的。
|| is C notation for logical OR: result is true if at least one part is true.
我希望printf能够运行或第二个特征ter是一个数字,但它只适用于第一个字符。
I'm expecting the printf to run if either the first or second character is a digit, but it only works for the first character.
那是因为你只读一个字符:
That is because you are reading only one character:
scanf("%c",pass);
读取字符串的格式为%s
,而%c
读取一个字符。所以你必须使用
The format to read strings is "%s"
while "%c"
reads a single character. So you have to use
scanf("%s",pass);
我是纠正了逻辑运算符,但问题出在%c上。将其更改为%s后,它可以正常工作。感谢您的回复
I've corrected the logic operator but the problem was with the %c. After changing it to %s it works fine. Thanks for the replies
这篇关于是否可以使用'和'& '或'与isupper和isdigit我正在尝试运行下面的代码,但它不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!