Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How does this function parse strings? I want to understand the theory

In this code I do not quite understand the lines
s += strspn (s, delim);
and
end = s + strcspn (s, delim);
As far as I know strspn returns the index of first occurrence of a delimiter in string.
Therefore if s is the string, then the delimiter is at index strcspn (s, delim)-1.
So, the command s += strspn (s, delim) would move the string pointer to the index after the delimiter.
Which means if I have a string s = "Abc,dsf.eerr" and delim = " ,.\n" and then if I execute this line, s += strspn (s, delim); then does that mean *s = ‘d’?
What is the significance of this line end = s + strcspn (s, delim);.
I need to implement strtok where the delimiter is not deleted but returned as a parameter. I just need to understand how this code works to do so. This might be a very basic question but still thanks for your help.

char *
__strtok_r (char *s, const char *delim, char **save_ptr)
{
  char *end;
  if (s == NULL)
    s = *save_ptr;
  if (*s == '\0')
    {
      *save_ptr = s;
      return NULL;
    }
  /* Scan leading delimiters.  */
  s += strspn (s, delim);
  if (*s == '\0')
    {
      *save_ptr = s;
      return NULL;
    }
  /* Find the end of the token.  */
  end = s + strcspn (s, delim);
  if (*end == '\0')
    {
      *save_ptr = end;
      return s;
    }
  /* Terminate the token and make *SAVE_PTR point past it.  */
  *end = '\0';
  *save_ptr = end + 1;
  return s;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Imagine it like this:

char *str = ",.,.,.DEF,.,.,.GAH";
char *s = str;
char *delim = ",.";

Now, you want to get to field DEF. So what you do:

To find the start of the field, you have to advance as long as there are delimiters:

  • s += strspn(s, delim)
  • strspn(s, delim) = strlen(",.,.,.") = 6
  • So *s = D

To find the end of the field from there, you have to advance as long as there are no delimiters:

  • end = s + strcspn(s, delim)
  • strcspn(s, delim) = strlen("DEF") = 3
  • So: *end = ','

Which means if I have a string s = "Abc,dsf.eerr" and delim = " ,.\n" and then if I execute this line, s += strspn (s, delim); then does that mean *s = ‘d’?

No, strspn("Abc,dsf.eerr", " ,.\n") = 0. A is not in " ,.\n".

What is the significance of this line end = s + strcspn (s, delim);

To find the end of the field, which we will be starting from on the next cycle.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading