jeudi 23 septembre 2021

Comparing Multiple Strings Using || Logical Operator Is Not Working Properly in C language

I am trying to check if the user string input (after lowercasing user input) matches with required three strings i.e. rock or paper or scissor. If it doesn't match the requirement the system will print It is a wrong input. Otherwise, I'll do something.

When I'm giving only one check without the logical || operator, it is working fine i.e. comparing the user input string with the required one. But when I'm using logical operator it is not working properly i.e. if I give right keyword, it is saying that it is a wrong input.

Being a beginner I couldn't figure out the possible reason after searching in StackOverflow also. Any help in advance. Thank you 🙏

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char game[40];
    printf("Enter one among rock, paper and scissor: ");
    scanf("%s", &game);
    for (int i = 0; game[i]; i++)
    {
        game[i] = tolower(game[i]);
    }
    if ((strcmp("rock", game) != 0) || (strcmp("paper", game) != 0) || (strcmp("scissor", game) != 0))
    {
        printf("You entered a wrong input\n");
    }
    else
    {
        /* Do Something */
    }
    
}

Aucun commentaire:

Enregistrer un commentaire