-2

I am trying to take a string as input and if it's equal to X++ or ++X, I would like to count it. for this purpose, I have written value code. Though it's working correctly for X++, for X-- it's not working. How can I solve the problem?

#include<stdio.h>
#include<string.h>
int main() {
    int n,count=0;
    char c[5];
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",c);
        if(strcmp(c,"X++")==1 || strcmp(c,"++X")==1) count++;
        else count--;
    }
    printf("%d",count);
    return 0;
}
Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
Md Nasir Ahmed
  • 23
  • 1
  • 10
  • It shouldn't be working correctly for either of them. What makes you think it is? – Barmar Jul 10 '21 at 07:58
  • 3
    Read closely the documentation of `strcmp`, especially what values it returns. Is it stated explicitely that `strcmp` can return `1`? – Jabberwocky Jul 10 '21 at 08:03

1 Answers1

2

You might want to change strcmp(c,"X++")==1 to strcmp(c,"X++")==0, and strcmp(c,"++X")==1 to strcmp(c,"++X")==0. strcmp() will return 0 if its arguments are equal, not 1. You can read more about strcmp() here.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312