Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9245693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:19:20+00:00 2026-06-18T09:19:20+00:00

I am trying to write a program which will output same printf if any

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:19:21+00:00Added an answer on June 18, 2026 at 9:19 am

    Your print statement doesn’t match the first sentence of your post or your if expression. 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:

    #include <stdio.h>
    #include <string.h>
    int main(int argc, char **argv)
    {
        char *string1 = argv[1];
        char *string2 = argv[2];
    
        if ((strcmp(string1, "test1") == 0) || (strcmp (string2, "test2") ==0)) 
             printf ("At least one string matched\n");
    
        return 0;
    }
    

    And output:

    $ ./example test1 bad
    At least one string matched
    $ ./example bad test2
    At least one string matched
    $ ./example bad bad
    $ ./example test1 test2
    At least one string matched
    

    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:

    int string1Matches = (strcmp(string1, "test1") == 0);
    int string2Matches = (strcmp(string2, "test2") == 0);
    
    if ((string1Matches && !string2Matches) || (!string1Matches && string2Matches))
        printf("Exactly one string matches (not both!)\n");
    

    Edit again:

    Your new program seems to work fine – what is your problem? Example output:

    $ ./example 
    Enter the first string: test1
    Enter the second string: bad
    Both strings are same
    $ ./example 
    Enter the first string: bad  
    Enter the second string: test2
    Both strings are same
    $ ./example 
    Enter the first string: test1
    Enter the second string: test2
    Both strings are same
    $ ./example 
    Enter the first string: bad
    Enter the second string: bad
    You didnt enter any matching 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program in Prolog, which will insert an element into
I am trying to write a Java program or Hadoop Pig script which will
I'm trying to write a program which will pseudorandomly autogenerate (based on a seed
I'm trying to write program which will switch tasks. Everything seems to work properly,
I am trying to write a program which will listen to the serial input
i am trying to write a scheme program which will take a list of
I'm trying to write very simple program which will imitate simple DeadLock, where Thread
I am trying to write a program that will accept user input which will
I'm trying to write a program which will run when a Windows Mobile device
I'm trying to write a simple program in VC++ which will just initialize the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.