简体   繁体   English

C - 如果有超过 1 个或

[英]C - if with more than 1 OR

I've written a function to check if a string array is a name (every char can only be a->z or A->Z or space) However if I run my program it doesn't work properly.我编写了一个函数来检查字符串数组是否是一个名称(每个字符只能是 a->z 或 A->Z 或空格)但是如果我运行我的程序,它就不能正常工作。 If I try to debug it, I see that it jumps to the line return 0;如果我尝试调试它,我会看到它跳转到 return 0 行; even if the first char in the array is 'M'.即使数组中的第一个字符是“M”。 Can anyone help me?谁能帮我?

int je_meno(char array[]) {
    int i=0;
    while(array[i]!=0){
        if(array[i]<32 || 32<array[i]<65 || 90<array[i]<97 || 122<array[i]){
            return 0;
        }
        i++;
    }
    return 1; }

Can it even compile the chain comparisons?它甚至可以编译链比较吗? Try simplifing the condition to something like:尝试将条件简化为:

if( !(array[i]>=65 && array[i] <=90 || array[i]>=97 && array[i]<=122 || array[i]==32) )
{
   return 0;
}

Basicaly if array[i] is NOT(capital character, small character or space) return 0基本上如果 array[i] 不是(大写字符、小字符或空格)返回 0

You could use functions isalpha() and isdigit() like this:您可以像这样使用函数 isalpha() 和 isdigit():

return !(isalpha(c) || isdigit(c) || (c == ' '));

Third expression is if you want to include spaces in your name.第三个表达式是如果你想在你的名字中包含空格。

So, line with if statement should be like this:所以,用 if 语句应该是这样的:

if !(isalpha(array[i]) || isdigit(array[i]) || (array[i] == ' ')) {

I think you'll need to include <ctype.h>.我认为您需要包含 <ctype.h>。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM