cant we compare two for inequality?

i mean if i have a char array 'a' and i want to know the number of digits in an array which is not '4' or not '7'

i.e if my array has value 4567778 the output should be 3 as there are three 7's and one 4 so output is 3.
my condition for check is (a!=52!!a!=55).
but this condition is never checked and if my condition is (a==52!!a==55) i got right output.

so that means we can only compare if the strings are equal?
this code give wrong output:


#include<stdio.h>

int main()

{
    int t,i,o;
    char a[100000];
    

scanf("%d",&t);
    while(t--)
    {
        i=0;
        o=0;
    scanf("%s",a);
    
    
    while(a[i]!=NULL)
    {
        
        if(a[i]!=52||a[i]!=55)      //this condition is not working?
         o++;
        i++;
    }

printf("%d",o);
}    

    return 0;
}

this works fine:

#include<stdio.h>
//#include<string.h>
//#include<conio.h>

using namespace std;

int main()

{
    int t,i,o;
    char a[100000];
    
//    cin>>t;
scanf("%d",&t);
    while(t--)
    {
        i=0;
        o=0;
    scanf("%s",a);
    
    
    while(a[i]!=NULL)
    {
        
        if(a[i]==52||a[i]==55)
         o++;
        i++;
    }

printf("%d ",i-o);
}    

    return 0;
}

Dani AI

Generated

Two separate, common C pitfalls explain the wrong result here: a boolean-logic error and an incorrect string-termination test.

Logic: using OR between two "not equal" checks makes the condition true for every possible character. For any character c, at least one of (c != '4') or (c != '7') is true, so the test never filters out '4' or '7'. The correct intent — "c is neither 4 nor 7" — requires both comparisons to hold simultaneously (see De Morgan's laws). Using readable character literals ('4' and '7') improves clarity versus raw ASCII codes.

String iteration and safety: stop at the string terminator ('\0') or use a library length function; comparing to NULL (the pointer value) is not the right check for a char. Also avoid unbounded input reads: prefer a bounded read or a safe input function to prevent buffer overflow.

Practical alternatives: either count occurrences of '4' and '7' and subtract from the total length, or increment a counter only when a character is neither '4' nor '7'. These fixes both correct the logic and make the code clearer. Credit to for pointing out the operator issue and to for noting alternative data layouts — a char array is fine for digit characters, just use the correct comparisons and termination check.

Recommended Answers

All 5 Replies

oopes the first line is :Cant we compare two strings for inequality?

char a[100000];

maybe you meant to use an array of integers since your just using numbers?

if(a!=52||a!=55) //this condition is not working?

Since you want to count those which are not 4 or which are not 7,
this should work according to me...

if(a[i]!=52 && a[i]!=55)

Use AND condition

@djsan...yeah you are right,my mistake..:)

Mark it solved if it is working

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.