Thread: Why is the output is not as expected?

  1. #1
    Registered User
    Join Date
    Jan 2022
    Posts
    2

    Why is the output is not as expected?

    Hi,
    I am trying to solve this question (on Hackerrank): https://www.hackerrank.com/challenge...month-week-one
    Basically, the user is asked to input a certain number of strings for 2 different arrays and then strings from one array are compared with strings on another array and if found, the number of times they occur are printed. For ex.:

    Input:
    4 //no. of strings in the first array
    aba
    baba
    aba
    xzxb //these are the 4 strings stored in one array
    3 //no. of strings in the second array
    aba
    xzxb
    ab //these are the 3 strings entered in another array

    Output (strings in 2nd array are compared with strings in the first array):
    2 //because "aba" appears twice in the first array
    1 //because "zxzb" appears once in the first array
    0 //because string "ab" does not appear in the first array

    But when I run the below code, I don't get the correct output (seems like the count is not getting to 0) I tried to debug but cannot find what is the problem:
    Code:
    #include<stdio.h>#include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    int main() {
    int n,q,count=0;
    char str[1000][20];
    char que[1000][20];
    
    scanf("%d",&n);
    for (int i=0;i<n;i++){
        scanf("%s",str[i]);
    }
    
    scanf("%d",&q);
    for (int i=0;i<q;i++){
        scanf("%s",que[i]);
    }
    
    for (int i=0;i<q;i++){
        for (int j=0;j<n;j++){
            if (strcmp(que[i],str[j])){
                count++;
            }
        }
        printf("%d\n",count);
        count=0;   
    } }
    If someone can help. Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Did you look up what strcmp returns, or did you just guess that it returns "true" when the strings are equal?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2020
    Posts
    150
    The problem is the line if(strcmp(que[i],str[j])){
    strcmp returns 0 if the string are equal.

  4. #4
    Registered User
    Join Date
    Jan 2022
    Posts
    2
    Oh my bad, I was thinking wrong the whole time and finding the problem in logic. Thanks @salem and @thmm !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. output value not as expected @beginner
    By nmn in forum C Programming
    Replies: 10
    Last Post: 03-20-2013, 03:55 PM
  2. Not getting expected Output
    By ldysmann in forum C++ Programming
    Replies: 5
    Last Post: 02-08-2012, 04:40 AM
  3. Replies: 9
    Last Post: 09-11-2011, 08:28 PM
  4. Array output not as expected
    By Futomara in forum C Programming
    Replies: 26
    Last Post: 11-04-2010, 11:24 AM
  5. non expected output
    By c++.prog.newbie in forum C Programming
    Replies: 2
    Last Post: 09-27-2004, 05:41 PM

Tags for this Thread