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

Find delimiter in strtok()

I am new to C programming.
I am trying to split a string using strtok(), using multiple delimiters. The line of code is :

char *token = strtok(st, " +-*/^()");

I want to know at which delimiter it got split. Is it possible? Please help.
I mean in this example whether the token got split at space, plus, minus, etc.

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

>Solution :

The function strtok changes the found delimiter by the zero character '\0'. So it is impossible to determine what delimiter was encountered.

Instead of the function strtok you can use functions strspn and strcspn. Using these functions you can determine what delimiter was encountered.

For example

size_t n = strcspn( st, " +-*/^()" );

if ( st[n] != '\0' )
{
    switch ( st[n] )
    {
    case ' ':
       //...
       break;
    case '+':
       //...
       break;
    //...
    }
}
  
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