954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

string problem

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[i]!=52!!a[i]!=55).
but this condition is never checked and if my condition is (a[i]==52!!a[i]==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;
}
shanki himanshu
Light Poster
37 posts since Dec 2010
Reputation Points: 9
Solved Threads: 1
 

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

shanki himanshu
Light Poster
37 posts since Dec 2010
Reputation Points: 9
Solved Threads: 1
 
char a[100000];

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

zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162
 
if(a[i]!=52||a[i]!=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

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

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

shanki himanshu
Light Poster
37 posts since Dec 2010
Reputation Points: 9
Solved Threads: 1
 

Mark it solved if it is working

DJSAN10
Posting Whiz in Training
249 posts since Dec 2010
Reputation Points: 38
Solved Threads: 26
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You