想要从双指针传递单个字符指针 [英] Want to pass a single char pointer from a double pointer

查看:119
本文介绍了想要从双指针传递单个字符指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须写一个函数,它接受2双指针(既char类型)。第一双指针有查询值的字符串:第二个有禁用词。这样做是为了消除来自查询字符串中的禁用词,返回所有的话没有这些停用词。

例如

输入 - 查询的,新,店,中,SF

 禁用词的,中

输出

商店
SF

我写了下面的code,而试图使用的strtok它接受唯一的单指向char类型。如何访问一个双指针的内容?

感谢

 的#include<&stdio.h中GT;无效remove_stopwords(字符**查询,INT query_length,焦炭**停用词,诠释stopwords_length){
    字符* final_str;    final_str =的strtok(查询[0],停止字[0]);
    而(final_str!= NULL)
    {
        的printf(%S \\ n,final_str);
        final_str =的strtok(NULL,禁用词);
    }}


解决方案

@Binayaka Chakraborty的解决方案解决了这个问题,但我想这可能是有用的规定,只有使用指针,显示适当使用的替代的strtok(),它的使用可能被误解的问题。

在特别的strtok()的第二个参数是指向列出要使用的所有单字符分隔符的字符串。一个不能使用的strtok()来分割基于多字符分隔符的字符串,因为似乎一直在这个问题的意图。

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdlib.h中GT;无效remove_stopwords(字符*查询,焦炭**禁用词){
    字符* final_str = strtok的(查询,);
    而(final_str!= NULL){
        INT ISSTOP = 0;
        焦炭** S;
        为(S =停用词; * S; S ++){
            如果(STRCMP(final_str,* S)== 0){
                ISSTOP = 1;
            }
        }
        如果的printf(%S,final_str)(ISSTOP!);
        final_str =的strtok(NULL,);
    }
}诠释主(){
    为const char * Q =新的SF店;
    字符*查询=的malloc(strlen的(Q)+1);
    / *我们复制字符串调用remove_stopwords()之前因为
       的strtok必须能够修改给定为它的第一个串
       参数* /
    的strcpy(查询,Q);
    字符*停用词[] = {,中,NULL};
    remove_stopwords(查询,禁用词);
    返回0;
}

此处所示的方法还避免了需要硬code所涉及的阵列,因此这减少了潜在的错误的大小。

I have to write a function which takes in 2 double pointers (both to char type). The first double pointer has a string of query values and the 2nd one has stopwords. The idea is to eliminate the stopwords from the query string and return all the words without those stopwords.

For example

Input - query: "the", "new", "store", "in", "SF"

    stopwords: "the", "in"

OUTPUT new store SF

I have written the following code while trying to use strtok which takes in only single pointers to char types. How do I 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);
    }

}

解决方案

@Binayaka Chakraborty's solution solved the problem but I thought it might be useful to provide an alternative that used pointers only and showed appropriate use of strtok(), the use of which may have been misunderstood in the question.

In particular, the second parameter of strtok() is a pointer to a string that lists all the single-character delimiters to be used. One cannot use strtok() to split a string based on multi-character delimiters, as appears to have been 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 avoids the need to hard code the sizes of the arrays involved, which therefore reduces potential for bugs.

这篇关于想要从双指针传递单个字符指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆