arrays - strstr() function in C -
i new learner , want output fill truth array use strstr() in c.
there 3 keyword groups in lists different size need compare.
keywords group 1: foo1,foo2. stored in truth[0],truth[1]. keywords group 2: bar1,bar2,bar3,bar4.stored in truth[2] truth[5]. keywords group 3: baz1,baz2,baz3,baz4,....stored in truth[6] truth[k-1].
all members in truth array set mask number (like -1) @ beginning assuming input not have keywords. if input matches keyword in keyword group, element in group should set 1 , other elements in same keywords group need set 0. want check there @ 1 keyword each keyword group.
for example:
input:"bar1"; output:[-1,-1, 1,0,0,0, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,....]; input:"foo1_baz5"; output:[1,0, -1,-1,-1,-1, 0,0,0,0,1,0,0,0,0,0,...]; input:"foo1_bar1_baz1"; output:[1,0, 1,0,0,0, 1,0,0,0,0,0,0,0,0,0,...]; input:"foo1_foo2"; output:"too many keywords".
this code have worked far (let's total 100 keywords in lists):
void fill_truth(char input, char *lists, int *truth) { int i; int truth[100]; memset(truth, -1, 100*sizeof(int)); int count = 0; for(i = 0; < 100; ++i){ if(strstr(input, lists[i])){ truth[i] = 1; ++count; } } if(count > 3) printf("too many keywords: %d, %s\n", count, input); }
how modify code strstr can set others 0 , check if no more 1 keyword same group? help!
update 1: sorry format issue, here -1 mask number, may use other integers other 1, , 0.
Update 2: right able set others 0 using 3 loops , can check total number of keywords, issue have @ moment give -nan if set initial values -1. tried other integers, overrides 0 whatever initial value set. hint why happens , initial value should set mask @ beginning?
update3: sorry misunderstanding due posting several variations of code when updating. figured out, , spent time in answering question!
Comments
Post a Comment