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 3788322
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:58:50+00:00 2026-05-19T11:58:50+00:00

I have a string like this: a;b;c;d;e f;g;h;i;j 1;2;3;4;5 and i want to parse

  • 0

I have a string like this:

a;b;c;d;e
f;g;h;i;j
1;2;3;4;5

and i want to parse it element by element. I used nested strtok function but it just splits first line and makes null the token pointer. How can i overcome this? Here is the code:

token = strtok(str, "\n");

while(token != NULL && *token != EOF)
{
    char a[128], b[128];
    strcpy(a,token);
    strcpy(b,a);
    printf("a:%s\n",a);
    char *token2 = strtok(a,";");
    while(token2 != NULL)
    {
        printf("token2 %s\n",token2);
        token2 = strtok(NULL,";");
    }
    strcpy(token,b);
    token = strtok(NULL, "\n");
    if(token == NULL)
    {
        printf("its null");
    }
}

Output:

token 2 a
token 2 b
token 2 c
token 2 d
token 2 e
  • 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-19T11:58:51+00:00Added an answer on May 19, 2026 at 11:58 am

    You cannot do that with strtok(); use strtok_r() from POSIX or strtok_s() from Microsoft if they are available, or rethink your design.

    char *strtok_r(char *restrict s, const char *restrict sep,
                   char **restrict lasts);
    char *strtok_s(char *strToken, const char *strDelimit, char **context); 
    

    These two functions are interchangeable.

    Note that a variant strtok_s() is specified in an optional part of C11 (Annex K in ISO/IEC 9899:2011). However, few suppliers other than Microsoft have implemented the interfaces in that section of the standard. The version of strtok_s() specified in Annex K has a different interface from Microsoft’s strtok_s() — similar problems bedevil a number of the other functions specified in Annex K.

    With strtok_r()

    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
        char str[] = "a;b;c;d;e\nf;g;h;i;j\n1;2;3;4;5\n";
        char *end_str;
        char *token = strtok_r(str, "\n", &end_str);
    
        while (token != NULL)
        {
            char *end_token;
            printf("a = %s\n", token);
            char *token2 = strtok_r(token, ";", &end_token);
            while (token2 != NULL)
            {
                printf("b = %s\n", token2);
                token2 = strtok_r(NULL, ";", &end_token);
            }
            token = strtok_r(NULL, "\n", &end_str);
        }
    
        return 0;
    }
    

    Results

    a = a;b;c;d;e
    b = a
    b = b
    b = c
    b = d
    b = e
    a = f;g;h;i;j
    b = f
    b = g
    b = h
    b = i
    b = j
    a = 1;2;3;4;5
    b = 1
    b = 2
    b = 3
    b = 4
    b = 5
    

    Without strtok_r()

    This works in context – provided that the data ends with a newline.

    #include <string.h>
    #include <stdio.h>
    
    int main(void)
    {
        char data[] = "a;b;c;d;e\nf;g;h;i;j\n1;2;3;4;5\n";
        char *string = data;
        char *token  = strchr(string, '\n');
    
        while (token != NULL)
        {
            /* String to scan is in string..token */
            *token++ = '\0';
            printf("a = %s\n", string);
            char *token2 = strtok(string, ";");
            while (token2 != NULL)
            {
                printf("b = %s\n", token2);
                token2 = strtok(NULL, ";");
            }
            string = token;
            token = strchr(string, '\n');
        }
    
        return 0;
    }
    

    Output

    a = a;b;c;d;e
    b = a
    b = b
    b = c
    b = d
    b = e
    a = f;g;h;i;j
    b = f
    b = g
    b = h
    b = i
    b = j
    a = 1;2;3;4;5
    b = 1
    b = 2
    b = 3
    b = 4
    b = 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string looking like this: 'Toy Story..(II) (1995)' I want to split the
I have a string like this in C#: string a = A|B|C|D I want
I have a string like this A. rahul VyAs and i want to remove
I have a string that looks like this: 9/1/2009. I want to convert it
I have a string that looks like this: <name>-<gender>-<age>.jpg I want to be very
I have a string like 'apples'. I want to find this string, and I
I have the string "re\x{0301}sume\x{0301}" (which prints like this: résumé) and I want to
I have an ordered list like this: var list = new List<String>(){aaa,aab,aac,baa,bab,bac}; I want
I have a string like this: 20090212 and I want to convert to valid
I have string like this:0:385 (first number is seconds, next numbers are milliseconds), I

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.