C 要从双指针传递单字符指针吗

C 要从双指针传递单字符指针吗,c,string,double-pointer,C,String,Double Pointer,我必须写一个函数,它接受两个双指针(都指向char类型)。第一个双指针有一个查询值字符串,第二个双指针有停止字。其思想是从查询字符串中删除stopwords,并返回没有这些stopwords的所有单词 比如说 输入-查询:“新”、“存储”、“中”、“SF” stopwords: “the”, “in” 输出 新的 商店 科幻小说 我在尝试使用strtok时编写了以下代码,strtok只接受指向char类型的单指针。如何访问双指针的内容 谢谢 #include <stdio.h&g

我必须写一个函数,它接受两个双指针(都指向char类型)。第一个双指针有一个查询值字符串,第二个双指针有停止字。其思想是从查询字符串中删除stopwords,并返回没有这些stopwords的所有单词

比如说

输入-查询:“新”、“存储”、“中”、“SF”

    stopwords: “the”, “in”
输出 新的 商店 科幻小说

我在尝试使用strtok时编写了以下代码,strtok只接受指向char类型的单指针。如何访问双指针的内容

谢谢

#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);
    }

}
#包括
void remove\u stopwords(字符**查询,整数查询长度,字符**停止字,整数停止字长度){
字符*最终字符;
final_str=strtok(查询[0],停止字[0]);
while(final_str!=NULL)
{
printf(“%s\n”,final_str);
final_str=strtok(空,停止字);
}
}

为了简单起见,可以假设双指针等同于2d数组(事实并非如此!)。但是,这意味着您可以使用数组约定访问双指针的内容

#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]));
        }
    }
}
#包括
#包括
char*query[5]={“the”、“new”、“store”、“in”、“SF”};
char*stopwords[2]={“the”,“in”};
char main_数组[256];
void remove\u stopwords(字符**查询,整数查询长度,字符**停止字,整数停止字长度);
int main()
{
删除_stopwords(查询,5,stopwords,2);
puts(主_数组);
返回0;
}
void remove\u stopwords(字符**查询,整数查询长度,字符**停止字,整数停止字长度)
{
int i,j,发现;

对于(i=0;i为了简单起见,您可以假设双指针等同于2d数组(不是!)。但是,这意味着您可以使用数组约定访问双指针的内容

#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]));
        }
    }
}
#包括
#包括
char*query[5]={“the”、“new”、“store”、“in”、“SF”};
char*stopwords[2]={“the”,“in”};
char main_数组[256];
void remove\u stopwords(字符**查询,整数查询长度,字符**停止字,整数停止字长度);
int main()
{
删除_stopwords(查询,5,stopwords,2);
puts(主_数组);
返回0;
}
void remove\u stopwords(字符**查询,整数查询长度,字符**停止字,整数停止字长度)
{
int i,j,发现;

对于(i=0;i@Binayaka Chakraborty的解决方案解决了这个问题,但我认为提供一个仅使用指针的替代方案可能会很有用,并且显示了对strtok()的适当使用,在这个问题中,使用strtok()可能会被误解

特别是,
strtok()
的第二个参数是指向一个字符串的指针,该字符串列出了要使用的所有单字符分隔符。不能使用
strtok()
根据多字符分隔符拆分字符串,这似乎是问题的意图

#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;
}
#包括
#包括
#包括
void remove_stopwords(char*query,char**stopwords){
char*final_str=strtok(查询“”);
while(final_str!=NULL){
int-isStop=0;
字符**s;
for(s=stopwords;*s;s++){
如果(strcmp(最终结构,*s)==0){
isStop=1;
}
}
如果(!isStop)printf(“%s”,最终字符);
final_str=strtok(空,“”);
}
}
int main(){
const char*q=“旧金山的新商店”;
char*query=malloc(strlen(q)+1);
/*我们在调用remove\u stopwords()之前复制字符串,因为
strtok必须能够修改作为第一个字符串的字符串
参数*/
strcpy(查询,q);
char*stopwords[]={“the”,“in”,NULL};
删除_stopwords(查询,stopwords);
返回0;
}

这里显示的方法还避免了对所涉及的数组大小进行硬编码的需要,从而减少了出现bug的可能性。

@Binayaka Chakraborty的解决方案解决了这个问题,但我认为提供一种仅使用指针的替代方案可能会很有用,并显示了对
strtok()的适当使用
,问题中可能对其使用有误解

特别是,
strtok()
的第二个参数是指向一个字符串的指针,该字符串列出了要使用的所有单字符分隔符。不能使用
strtok()
根据多字符分隔符拆分字符串,这似乎是问题的意图

#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;
}
#包括
#包括
#包括
void remove_stopwords(char*query,char**stopwords){
char*final_str=strtok(查询“”);
while(final_str!=NULL){
int-isStop=0;
字符**s;
for(s=stopwords;*s;s++){
如果(strcmp(最终结构,*s)==0){
isStop=1;
}
}
如果(!isStop)printf(“%s”,最终字符);
final_str=strtok(空,“”);
}
}
int main(){
const char*q=“旧金山的新商店”;
char*query=malloc(strlen(q)+1);
/*我们在调用remove\u stopwords()之前复制字符串,因为
strtok必须能够修改作为第一个字符串的字符串
参数*/
strcpy(查询,q);
char*stopwords[]={“the”,“in”,NULL};
删除_stopwords(查询,stopwords);
返回0;
}

此处显示的方法还避免了对所涉及数组的大小进行硬编码的需要,从而减少了出现错误的可能性。

因此,如果我想将查询内容检索到单个字符指针中,我可以使用语法*query[i]访问它吗?@user2555541:您可以将结果连接到一个单字符指针谢谢您的帮助。我该怎么做?同样,使用strcat只需要访问单字符ptr。实际上这就是我一直在尝试的。将双ptr的内容移动到一个单字符*中,然后使用strok函数消除分隔符。如果我想将查询内容检索到单个字符指针中,我可以使用语法*query[i]访问它吗?@user2555541:您可以将结果连接到单个字符指针谢谢您的帮助。我该怎么做?同样,使用