you need to adjust your test if(-1)
C is true , and strcmp()
does not return true or false , it return a number positive or negative , and zero if the two strings match, this means that if(strcmp(str2[i],ptr)
is always true except when str2[i] matches ptr, you can resolve this by using somthing like this to find matches if(strcmp(str2[i],ptr) == 0)
or if(!strcmp(str2[i],ptr))
which return true only if strcmp return 0 ,your test is equivalent to if(strcmp(str2[i],ptr) != 0)
the reason why you can see sun
in your results is : sun
does not match with Test
and raised
,
if you are trying to find strings that does not exist only, update your code to this
int main()
{
char str1[50]="TEST sun raised";
char str2[4][90]={"sun","in"};
char delim[] = " ";
char *ptr=strtok(str1,delim);
while(ptr!=NULL){
int i=0;
int found = 0;
for( i=0;i<4;i++){
if(strcmp(str2[i],ptr)==0){
found++;
break;
}
}
if(found == 0) printf("%s does not exist \n",ptr);
ptr=strtok(NULL,delim);
}