Thread: How exactly is strspn and strscpn used in spliting up a string here

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    228

    How exactly is strspn and strscpn used in spliting up a string here

    So I found a bit of code over in stackoverflow that I found a bit difficult to understand. What it does is it splits a string based on a delimiter and then stores the rest in a pointer while returning the part that was split.

    Here it is

    Code:
    char *strtok_r(char *str, const char *delim, char **nextp)
    {
        char *ret = NULL;
    
    
        if (str == NULL)
        {
            str = *nextp;
        }
    
    
        str += strspn(str, delim); // This part I don't understand
    
    
        if (*str == '\0')
        {
            return NULL;
        }
    
    
        ret = str;
    
    
        str += strcspn(str, delim); // and this part
    
    
        if (*str)
        {
            *str++ = '\0';
        }
    
    
        *nextp = str;
    
    
        return ret;
    }
    what strspn does is returns the length of the initial portion of str1 containing only characters that appear in str2 and strcspn does the opposite. The function's return type is size_t and not something like char *. So how is it that at the end of this function it is able to get the delimited part of the string based on the length that it got from strspn and strscpn?
    Last edited by deathslice; 04-01-2016 at 08:56 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    The first part skips over a set of delimiters

    The second part skips over a set of non-delimiters.

    > So how is it that at the end of this function it is able to get the delimited part of the string based on the length that it from strspn and strscpn?
    That's what lines 21 and 29 do.

    Or you could copy the code into a project of your own, then step through the code using a debugger to study it's behaviour.

    Or old school - add several debugging printf statements at interesting points in the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    228
    That is what I did but I saw no interesting change. Only that at the end it got the delimited part. What is really vexing is the part where it is doing += and assigning a size_t type to char *. When that line happened while I was debugging, there was no explicit change.

    Edit: Never mind. Though still how is it ok to do this str += strspn(str, delim);?
    Last edited by deathslice; 04-01-2016 at 09:40 AM.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by deathslice View Post
    how is it ok to do this str += strspn(str, delim);?
    How is it okay to do str++? What's the difference? You're simply adding a number to a pointer. strspn returns the number of chars from the start of str that match chars in delim. Adding that to str causes it to point to the first char in str that is not in delim.

    So it's not assigning a size_t to a char *, it's incrementing a char* by a size_t, a very different thing.

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    228
    oh ok I see.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spliting up a string of numbers.
    By UltimateCoder in forum C++ Programming
    Replies: 23
    Last Post: 01-26-2012, 02:32 AM
  2. Spliting in group of two
    By RisingACK in forum C Programming
    Replies: 6
    Last Post: 11-10-2010, 12:00 PM
  3. reading and spliting a string
    By nitinjainb in forum C++ Programming
    Replies: 5
    Last Post: 06-24-2004, 08:35 AM
  4. strspn()
    By JDMac in forum C Programming
    Replies: 4
    Last Post: 11-04-2002, 04:01 PM
  5. int StrSpn(char *str, int ch);
    By Krush in forum C Programming
    Replies: 5
    Last Post: 11-01-2002, 07:13 PM