Caleb Jay
Caleb Jay

Reputation: 2199

Using strchr() to create multiple strings from one master string

I'm working on a program in C for a class that requires me to take a request-line and break it down into its subsequent pieces. It's for learning purposes, so I can expect a fairly standard request-line.

Thinking about the problem, I was going to march through each char in the request-line with some sort of for() loop that creates a new string every time it encounters a SP, but I was wondering if there was a way to use strchr() to point to each "piece" of the request-line?

Since a request-line looks like method SP request-target SP HTTP-version CRLF where SP is a single space, is there some way I could create a function that uses strchr(request-line, ' ') to create a string (or char* or char[ ]) that then ENDS at the next SP ?

edit:

So I could do something like

char method = strchr(request-line, ' ');

But then, wouldn't "method" be every char after the SP? How can I "trim" what gets put into my variable? Or am I totally misunderstanding how this function works?

Upvotes: 1

Views: 1185

Answers (2)

Weather Vane
Weather Vane

Reputation: 34585

This shows how to use the strchr() function you mentioned. I assume there is a single space between each element of the string as you say. I have glossed over a few things, as I don't provide a complete homework answer. One of them is whether the space before the CRLF you show does exist - I assume it does as you show it, but if not, you'll have to deal with that, perhaps by using strcspn() instead of strchr(). The other glosss is to assume a maximum length, in real life you would malloc() (and later free()) the memory required by substring.

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

#define MAXLEN 100

int main(void)
{
    char input[] = "method request-target HTTP-version \r\n";
    char substring[MAXLEN+1];
    char *sptr, *lptr;
    int len;

    lptr = input;                                   // start of search
    while ((sptr = strchr(lptr, ' ')) != NULL) {
        len = sptr - lptr;                          // length of phrase
        if (len > MAXLEN)
            return 0;                               // simple precaution
        memcpy(substring, lptr, len);               // copy the substring
        substring[len] = 0;                         // nul-terminate it
        printf("Substring is '%s'\n", substring);   // tell us what it is
        lptr = sptr + 1;                            // advance to next search
    }
    return 0;
}

Program output:

Substring is 'method'
Substring is 'request-target'
Substring is 'HTTP-version'

Upvotes: 1

Phil Krylov
Phil Krylov

Reputation: 530

You can technically use strtok but it will modify the request line in place, which may be acceptable, but not in every situation. Here is a generalized solution:

char *method, *target, *version;
const char *p = request_line, *p1;

while (*p != ' ')
{
   p++;
}
method = strndup(request_line, p - request_line);
p1 = ++p;
while (*p != ' ')
{
   p++;
}
target = strndup(p1, p - p1);
p1 = ++p;
while (*p != '\r')
{
   p++;
}
version = strndup(p1, p - p1);

As you expect only well-formatted input, I omitted all error checks.

Upvotes: 2

Related Questions