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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:25:59+00:00 2026-06-09T16:25:59+00:00

char line[255]; char *token = NULL; char *line2 = NULL; char *temporaryToken = NULL;

  • 0
char line[255];
char *token = NULL;
char *line2 = NULL;
char *temporaryToken = NULL;

if( scanf(" %[^\n]", line) > 0)
    token = strtok( line, ";" ); //divide the line by ;
    do
    {
        line2 = token;
        temporaryToken = strtok(line2, " ");
        do
        {
            //divide the line2 by spaces into command and args, not the question here]
            temporaryToken = strtok( NULL, " " );
        }while (temporaryToken != NULL );
        token = strtok( NULL, ";" );
    }while(token != NULL);

this is not my code verbatim, by the way, just an example of how it’s set out

In my program, when I print the “token” variable before I split a second time, it’ll print out everything until the ; character.

For example, say stdIn took in “ls -la; mkdir lololol; ls -la”, it would print “ls -la”. But then, after the second split, printing “token” would only print “ls”.

Why is this, and how could I go about fixing it?

  • 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-06-09T16:26:01+00:00Added an answer on June 9, 2026 at 4:26 pm

    There are two problems with strtok().

    1. It modifies its input string.
    2. Only one set of strtok() calls can be active at a time.

    I think your problem is the latter. You also have an indentation problem in the code:

    if (scanf(" %[^\n]", line) > 0)
        token = strtok( line, ";" );
    do
    {
        line2 = token;
        temporaryToken = strtok(line2, " ");
        do
        {
            //divide the line2 by spaces into command and args, not the question here]
            temporaryToken = strtok(NULL, " ");
        } while (temporaryToken != NULL);
        token = strtok( NULL, ";" );
    } while(token != NULL);
    

    You probably intended it to read:

    if (scanf(" %[^\n]", line) > 0)
    {
        token = strtok(line, ";");
        do
        {
            line2 = token;
            temporaryToken = strtok(line2, " ");
            do
            {
                //divide the line2 by spaces into command and args, not the question here]
                temporaryToken = strtok(NULL, " ");
            } while (temporaryToken != NULL);
            token = strtok(NULL, ";");
        } while (token != NULL);
    }
    

    Assuming this is what you intended, you still have the problem that there is one strtok() running on line, and then a second one running on line2. The trouble is, the loop on line2 completely wrecks the interpretation of line. You can’t use the nested loops with strtok().

    If you must use something like strtok(), then look for either POSIX strtok_r() or Microsoft’s strtok_s() (but note that the C11 standard Annex K version of strtok_s() is different — see Do you use the TR 24731 ‘safe’ functions?).

    if (scanf(" %[^\n]", line) > 0)
    {
        char *end1;
        token = strtok_r(line, ";", &end1);
        do
        {
            char *end2;
            line2 = token;
            temporaryToken = strtok_r(line2, " ", &end2);
            do
            {
                //divide the line2 by spaces into command and args, not the question here]
                temporaryToken = strtok_r(NULL, " ", &end2);
            } while (temporaryToken != NULL);
            token = strtok_r(NULL, ";", &end1);
        } while (token != NULL);
    }
    

    About the Comments

    While you use strtok() or one of its relatives, the input string will be modified, and if you have multiple delimiters, you will not be able to tell which delimiter was present. You can work with a copy of the string, and do comparisons (usually based on offsets from the start of the string).

    Within the limits of using strtok_r(), the solution above ‘works’. Here’s a test program to demonstrate:

    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char line[1024];
    
        if (scanf(" %[^\n]", line) > 0)
        {
            char *end1;
            char *token;
            printf("Input: <<%s>>\n", line);
            token = strtok_r(line, ";", &end1);
            do
            {
                char *end2;
                char *line2 = token;
                char *temporaryToken;
                printf("Token1: <<%s>>\n", token);
                temporaryToken = strtok_r(line2, " ", &end2);
                do
                {
                    printf("Token2: <<%s>>\n", temporaryToken);
                    //divide the line2 by spaces into command and args, not the question here]
                    temporaryToken = strtok_r(NULL, " ", &end2);
                } while (temporaryToken != NULL);
                token = strtok_r(NULL, ";", &end1);
            } while (token != NULL);
        }
    
        return 0;
    }
    

    Example input and output:

    $ ./strtok-demo
    ls -la; mkdir lololol; ls -la
    Input: <<ls -la; mkdir lololol; ls -la>>
    Token1: <<ls -la>>
    Token2: <<ls>>
    Token2: <<-la>>
    Token1: << mkdir lololol>>
    Token2: <<mkdir>>
    Token2: <<lololol>>
    Token1: << ls -la>>
    Token2: <<ls>>
    Token2: <<-la>>
    $
    

    Alternative using strcspn() and strspn()

    If you don’t want to demolish the original string, you must use other functions than the strtok() family. The functions strcspn() and strspn() are suitable; they are part of Standard C (C89 and later versions), albeit much less well known than some of the other functions. But they’re spot on for this task.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    static char *substrdup(const char *src, size_t len);
    
    int main(void)
    {
        char line[1024];
    
        if (scanf(" %[^\n]", line) > 0)
        {
            char *start1 = line;
            size_t len1;
            printf("Input: <<%s>>\n", line);
            while ((len1 = strcspn(start1, ";")) != 0)
            {
                char *copy = substrdup(start1, len1);
                char *start2 = copy;
                size_t len2;
                printf("Token1: %zd <<%.*s>>\n", len1, (int)len1, start1);
                printf("Copy: <<%s>>\n", copy);
                start2 += strspn(start2, " ");      // Skip leading white space
                while ((len2 = strcspn(start2, " ")) != 0)
                {
                    printf("Token2: %zd <<%.*s>>\n", len2, (int)len2, start2);
                    start2 += len2;
                    start2 += strspn(start2, " ");
                }
                free(copy);
                start1 += len1;
                start1 += strspn(start1, ";");
            }
            printf("Check: <<%s>>\n", line);
        }
    
        return 0;
    }
    
    #include <assert.h>
    
    static char *substrdup(const char *src, size_t len)
    {
        char *copy = malloc(len+1);
        assert(copy != 0);              // Apalling error handling strategy
        memmove(copy, src, len);
        copy[len] = '\0';
        return(copy);
    }
    

    Example input and output:

    $ strcspn-demo
    ls -la; mkdir lololol; ls -la
    Input: <<ls -la; mkdir lololol; ls -la>>
    Token1: 140734970342872 <<>>
    Copy: <<ls -la>>
    Token2: 2 <<ls>>
    Token2: 3 <<-la>>
    Copy: << mkdir lololol>>
    Token2: 5 <<mkdir>>
    Token2: 7 <<lololol>>
    Copy: << ls -la>>
    Token2: 2 <<ls>>
    Token2: 3 <<-la>>
    Check: <<ls -la; mkdir lololol; ls -la>>
    $
    

    This code goes back to the more comfortable while loop, rather than needing to use do-while loops, which is a benefit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I read a char from a user in the command line. I
#include<stdio.h> #include<math.h> int main(int argc, char **argv){ // read in the command-line argument double
LIST *list; list = createList(); FILE *file = fopen(test.txt,r); char line[50]; char* token; while(fgets(line,sizeof(line),file))
How can I use sed to replace this line char * path_list_[1] = {
I tried the following line: static const const char* values[]; But I get the
I have the following code: char stats[109]; /* !LINE UNDER QUESTION! */ sprintf(stats, OBJECTS:\n%u/256\n
Consider this line: std::wcout << Hello World!; Is it OK to pass char* or
Why does the below code give Seg. Fault at last line? char* m=ReadName(); printf(\nRead
char myData[505][3][50]; //2D array, each 50 chars long char **tableData[505] = {NULL}; const char*
My code is as follows: #include <stdio.h> struct MyData { int id; char msg[255];

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.