@Binayaka Chakraborty's solution solved the problem, but I thought it would be useful to provide an alternative that used only pointers and showed the appropriate use of strtok() , the use of which might have been misunderstood in the question.
In particular, the second strtok() parameter is a pointer to a string that lists all the single-character delimiters that will be used. You cannot use strtok() to split a string based on multi-character delimiters, which apparently was the intention in the question.
#include <stdio.h> #include <string.h> #include <stdlib.h> void remove_stopwords(char *query, char **stopwords) { char *final_str = strtok(query, " "); while(final_str != NULL) { int isStop = 0; char **s; for (s = stopwords; *s; s++) { if (strcmp(final_str,*s) == 0) { isStop = 1; } } if (!isStop) printf("%s ", final_str); final_str = strtok(NULL, " "); } } int main() { const char *q = "the new store in SF"; char *query = malloc(strlen(q)+1); /* We copy the string before calling remove_stopwords() because strtok must be able to modify the string given as its first parameter */ strcpy(query,q); char *stopwords[] = {"the", "in", NULL}; remove_stopwords(query,stopwords); return 0; }
The approach shown here also eliminates the need for hard coding of the sizes of the arrays involved, which therefore reduces the likelihood of errors.
source share