试图获取子字符串,但程序返回空字符串

时间:2016-09-02 06:36:21

标签: c string substring

在下面的代码中,在“parse”函数中,我试图从字符串“line”获取子字符串。我正在成功打印“方法”变量,但“requesttarget”和“httpversion”变量由于某种原因是空的。

(ps所有这些printf也在我的解析函数中)

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


    //prototypes
    bool parse(const char* line, char* abs_path, char* query);
    int strindex(char** pos, const char* str);
    void substr(int start, int end, char* holder, const char* line);

    int main(void)
    {
        const char* line = "GET /hello.php?name=Alice HTTP/1.1";   
        char* abs_path = NULL;
        char* query = NULL;

        if(parse(line, abs_path, query))
        {
            printf("It works!\n");
        }
    }

    bool parse(const char* line, char* abs_path, char* query)
    {
        char* space;
        int firstspace;
        int secondspace;
        char* method = malloc(50 * sizeof(char));
        char* requesttarget = malloc(50 * sizeof(char));
        char* httpversion = malloc(50 * sizeof(char));

        space = strchr(line, ' ');

        printf("%p\n", space);

        //checks if strchr returns
        if(space == NULL)
        {
            return false;
        }

        //index in INT of the character
        firstspace = strindex(&space, line);

        printf("%i\n", firstspace);

        //stores the method
        substr(0, firstspace, method, line);

        space = strrchr(line, ' ');

        printf("%p\n", space);

        //index in INT of the character
        secondspace = strindex(&space, line);

        printf("%i\n", secondspace);

        //checks if strchr returns
        if(space == NULL)
        {
            return false;
        }

        //firstspace should come before secondspace
        if(firstspace > secondspace)
        {
            return false;
        }

        //stores request - target
        substr(firstspace + 1, secondspace, requesttarget, line);

        //stores http-version
        substr(secondspace + 1, strlen(line), httpversion, line);

        printf("method: %s\n", method);
        printf("requesttarget: %s\n", requesttarget);
        printf("httpversion: %s\n", httpversion);

        return true;
    }


    int strindex(char** pos, const char* str)
    {
        for(int i = 0, n = strlen(str); i < n; i++)
        {
            if((str + i) == *pos)
            {
                return i;
            }
        }
        return -1;
    }

    void substr(int start, int end, char* holder, const char* line)
    {
        //char* holder = malloc(50 * sizeof(char));
        int i = start;
        for(; i < end; i++)
        {
            holder[i] = line[i];
        }
        holder[i] = '\0';
        //return holder;
    }

2 个答案:

答案 0 :(得分:1)

 void substr(int start, int end, char* holder, const char* line)
    {
        //char* holder = malloc(50 * sizeof(char));
        int i = start, j=0;
        for(; i < end; i++)
        {
            holder[j++] = line[i];
        }
        holder[j] = '\0';
        //return holder;
    }

您没有正确地在第二次迭代中将数据存储在 holder 中。 从第二次迭代 start = 3 end = 25 。存储在 holder 中时,您的索引从3开始,这对于是正确的,而对于 holder 则不正确。 添加一个变量以从0开始 holder 的索引。

答案 1 :(得分:0)

较小的版本:(未经测试)

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

int main(void)
{
    const char* line = "GET /hello.php?name=Alice HTTP/1.1";   
    char method[32], request[1024], version[32], *src, *dest, *end;

    for(src=line, end=(dest=method)+sizeof(method)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
    *dest='\0';
    while(*src==' ') src++;
    for(end=(dest=request)+sizeof(request)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
    *dest='\0';
    while(*src==' ') src++;
    for(end=(dest=version)+sizeof(version)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
    *dest='\0';

    printf("method: %s\n", method);
    printf("requesttarget: %s\n", request);
    printf("httpversion: %s\n", version);
}