相关疑难解决方法(0)

C中的strtok函数如何工作?

我找到了这个解释strtok函数的示例程序:

#include <stdio.h>
#include <string.h>

int main ()
{
    char str[] ="- This, a sample string.";
    char * pch;
    printf ("Splitting string \"%s\" into tokens:\n",str);
    pch = strtok (str," ,.-");
    while (pch != NULL)
    {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道这是如何工作的.

如何pch = strtok (NULL, " ,.-");返回一个新令牌呢?我的意思是,我们要求strtokNULL.这对我来说没有多大意义.

c strtok

20
推荐指数
2
解决办法
2万
查看次数

在for循环中使用strtok

要遍历我使用的字符串str:

for (tok = strtok(str, ";"); tok && *tok; tok = strtok(NULL, ";"))  
{  
   //do stuff  
}
Run Code Online (Sandbox Code Playgroud)

我想了解这个循环是如何工作的.在我看来:

(1) tok = strtok(str, ";"); //initialization of tok with the first token in str    
(2) tok = strtok(NULL, ";"); // go to the next token in str? how does this work? 
(3) tok && *tok;  //this stops the loop when tok =NULL or *tok=NULL 
Run Code Online (Sandbox Code Playgroud)

非常感谢你的帮助!

c

6
推荐指数
1
解决办法
2882
查看次数

strtok() 函数的实现

我需要编写我的函数strtok。下面是我的代码。问题是 - 我无法显示结果字符串。在我使用的代码中strcpy(),然后显示新数组。是否可以仅使用指针显示字符串*str

#include <stdio.h>
#include <string.h>   

char* my_strtok(char* s, char* delm){
    char W[100];
    int i = 0, k = 0, j = 0;
    char *ptr;
    static char *Iterator;
    ptr = s;

    if (s == NULL){
        s = Iterator;
    }
    while (s[i] != '\0'){
        j = 0;
        while (delm[j] != '\0'){
            if (s[i] != delm[j])
                W[k] = s[i];
            else goto It;
            j++;
        }
        ptr++;
        i++;
        k++;
    }
It:
    W[i] = 0;
    Iterator = ++ptr;
    return W; …
Run Code Online (Sandbox Code Playgroud)

c strtok

3
推荐指数
2
解决办法
4万
查看次数

通过回车拆分C

我有一个问题,我试图通过Web代理的回车分割HTTP请求.请求似乎没有拆分.

以下是一个示例请求:GET /pub/WWW/TheProject.html HTTP/1.1\r \nHost:www.w3.org\r \n

我的尝试是:

char* split_request;
split_request = strtok(request, "\r\n");
Run Code Online (Sandbox Code Playgroud)

但它永远不会分裂?我不确定我错过了什么.当我使用wget或浏览器测试Web代理时,它似乎分裂了,但是没有使用telnet.

c string split httprequest webproxy

2
推荐指数
1
解决办法
8136
查看次数

打印出位于动态内存中的数组元素

XXX ----是的,这是一个家庭作业的问题,但是我被卡住了.为什么不打印出数组的元素?请帮帮--- XXX

好的,我们得到了打印输出部分.非常感谢.现在问题是空格分隔符放入数组之前唯一的第一个字符.我需要将所有单词或字符设置到数组中.

int main(int argc, char** argv) {

    int size = 0;
    char **array = malloc(0); //malloc for dynamic memory since the input size is unknown
    static const char filename[] = "input.txt";
    FILE *file = fopen(filename, "r");
    if (file != NULL) {
        char line [ 128 ]; 

        char delims[] = " ";
        char *result = NULL;
        while (fgets(line, sizeof line, file) != NULL)  {
            result = strtok(line, delims); //separate by space
            size++;
            array = realloc(array, size * sizeof …
Run Code Online (Sandbox Code Playgroud)

c arrays malloc dynamic

1
推荐指数
1
解决办法
8621
查看次数

标签 统计

c ×5

strtok ×2

arrays ×1

dynamic ×1

httprequest ×1

malloc ×1

split ×1

string ×1

webproxy ×1