Want to pass a single char pointer from a double pointer

I need to write a function that takes 2 double pointers (both char types). The first double pointer has a string of query values, and the second has stop words. The idea is to exclude stop words from the query string and return all words without these stops.

for instance

Input request: "", "new", "store", "in", "SF"

stopwords: "the", "in" 

CONCLUSION new SF store

I wrote the following code when trying to use strtok, which accepts only single pointers to char types. How to access the contents of a double pointer?

thanks

 #include <stdio.h> void remove_stopwords(char **query, int query_length, char **stopwords, int stopwords_length) { char *final_str; final_str = strtok(query[0], stopwords[0]); while(final_str != NULL) { printf("%s\n", final_str); final_str = strtok(NULL, stopwords); } } 
0
source share
2 answers

For simplicity, you can assume that a double pointer is equivalent to a 2d array (it is not!). However, this means that you can use the array convention to access the contents of the double pointer.

 #include <stdio.h> #include <string.h> char *query[5] = {"the","new","store","in","SF"}; char *stopwords[2] = {"the","in"}; char main_array[256]; void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length); int main() { remove_stopwords(query,5,stopwords,2); puts(main_array); return 0; } void remove_stopwords(char **query,int query_length, char **stopwords, int stopwords_length) { int i,j,found; for(i=0;i<query_length;i++) { found=0; for(j=0;j<stopwords_length;j++) { if(strcmp(query[i],stopwords[j])==0) { found=1; break; } } if(found==0) { printf("%s ",query[i]); strncat(main_array,query[i],strlen(query[i])); } } } 

Output: new store SF newstoreSF

+2
source

@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.

+2
source

All Articles