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!