简体   繁体   English

C中的字符比较?

[英]Char comparison in c?

I am trying the following code to try to find whether there is a comma followed by a semicolon in a char array. 我正在尝试下面的代码来尝试查找char数组中是否有逗号后跟分号。 Here is my code 这是我的代码

char m[80];
char *p = m;

while(p!=pend)
{
    char *pend = m + strlen(m);  
    int i=0;

    if(*p!=',' || *p!=';')
    {
        printf("DDD");
        char temp[2];
        temp[0] = *p;
        temp[1] = '\0';
        strcat( m2, temp );
    }
    else if(*p==',' && (*(p+1)==';'))
    {
        printf("CCC");
        char temp[2];
        temp[0] = *p;
        temp[1] = '\0';
        strcat( m2, temp );
    }
}

But the problem is, eve though input is, for example 12,;3 it never enters "else if" part. 但是问题是,即使输入是例如12 ;; 3,它也永远不会输入“ else if”部分。 What can i do about it? 我该怎么办?

Thank you 谢谢

它不会进入else部分,因为if部分始终求值为true( 任何字符都不等于','';' )。

if(*p!=',' || *p!=';')

will always be true ( *p can't be both ',' and ';' ). 将始终为true( *p不能同时为 ','';' )。 Since the first if is always true, the following else if is never evaluated. 由于第一个if始终为true,因此后续的else if永远不会求值。

I'm not sure what you're trying to do with the first if block. 我不确定您要对第一个if块做什么。 Maybe the simplest 'fix' would be to just delete that part of your code. 也许最简单的“修复”是删除代码的那部分。

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

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