I am trying to write a program which will output same printf if any one of the string match. I tried following but it doesn’t working for me. Here I did compare first string or second string, if any one is same then it should print the statement listed in printf.
#include <stdio.h>
#include <string.h>
int main (){
char string1[10];
char string2[10];
printf("Enter the first string: ");
scanf ("%s", string1);
printf("Enter the second string: ");
scanf ("%s", string2);
if ((strcmp(string1, "test1") == 0) || (strcmp (string2, "test2") ==0))
printf ("Both strings are same\n");
else printf("You didnt enter any matching \n");
}
What am I missing here?
Your print statement doesn’t match the first sentence of your post or your
ifexpression. If you want to test that both are equal, you should be using&&rather than||. If you want to test if either of the strings matches your test strings, your program is fine. You must have a problem with a different part of your code. Here’s an example program to prove it for you:And output:
Edit: It occurs to me on further reading that you actually might want to be testing to see if exactly one of them matches. In that case, you’ll need a different expression in your
if. Maybe something like:Edit again:
Your new program seems to work fine – what is your problem? Example output: