Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7000333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:39:25+00:00 2026-05-27T20:39:25+00:00

I am trying to tokenize a string but I need to know exactly when

  • 0

I am trying to tokenize a string but I need to know exactly when no data is seen between two tokens. e.g when tokenizing the following string “a,b,c,,,d,e” I need to know about the two empty slots between ‘d‘ and ‘e‘… which I am unable to find out simply using strtok(). My attempt is shown below:

char arr_fields[num_of_fields];
char delim[]=",\n";
char *tok;
tok=strtok(line,delim);//line contains the data

for(i=0;i<num_of_fields;i++,tok=strtok(NULL,delim))
{
    if(tok)
        sprintf(arr_fields[i], "%s", tok);
    else
        sprintf(arr_fields[i], "%s", "-");          
}

Executing the above code with the aforementioned examples put characters a,b,c,d,e into first five elements of arr_fields which is not desirable. I need the position of each character to go in specific indexes of array: i.e if there is a character missing between two characters, it should be recorded as is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:39:26+00:00Added an answer on May 27, 2026 at 8:39 pm

    7.21.5.8 the strtok function

    The standard says the following regarding strtok:

    [#3] The first call in the sequence searches the string
    pointed to by s1 for the first character that is not
    contained in the current separator string pointed to by s2.
    If no such character is found, then there are no tokens in
    the string pointed to by s1 and the strtok function returns
    a null pointer. If such a character is found, it is the
    start of the first token.

    In the above quote we can read you cannot use strtok as a solution to your specific problem, since it will treat any sequential characters found in delims as a single token.


    Am I doomed to weep in silence, or can somebody help me out?

    You can easily implement your own version of strtok that does what you want, see the snippets at the end of this post.

    strtok_single makes use of strpbrk (char const* src, const char* delims) which will return a pointer to the first occurrence of any character in delims that is found in the null-terminated string src.

    If no matching character is found the function will return NULL.


    strtok_single

    char *
    strtok_single (char * str, char const * delims)
    {
      static char  * src = NULL;
      char  *  p,  * ret = 0;
    
      if (str != NULL)
        src = str;
    
      if (src == NULL)
        return NULL;
    
      if ((p = strpbrk (src, delims)) != NULL) {
        *p  = 0;
        ret = src;
        src = ++p;
    
      } else if (*src) {
        ret = src;
        src = NULL;
      }
    
      return ret;
    }
    

    sample use

      char delims[] = ",";
      char data  [] = "foo,bar,,baz,biz";
    
      char * p    = strtok_single (data, delims);
    
      while (p) {
        printf ("%s\n", *p ? p : "<empty>");
    
        p = strtok_single (NULL, delims);
      }
    

    output

    foo
    bar
    <empty>
    baz
    biz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm trying to tokenize a string by loading an entire file into a
I'm trying to create a regex to tokenize a string. An example of the
I'm trying to adapt this answer How do I tokenize a string in C++?
I am trying to tokenize a string using the pattern as below. >>> splitter
I am trying to tokenize a database dump separated by commas. I only need
I'm trying to parse a simple string in C++. I know the string contains
I am trying to read data from a file, tokenize it and sort it,
I am trying to build a string tokenizer that can tokenize on mutilple characters.
I'm trying to tokenize a string with multiple spaces. For example, yes___no , where
I am trying to read contents of a file using string tokenizer and store

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.