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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:50:11+00:00 2026-05-25T15:50:11+00:00

I am trying to work with strtok and strcat but the second printf never

  • 0

I am trying to work with strtok and strcat but the second printf never shows up. Here is the code:

int i = 0;
char *token[128];
token[i] = strtok(tmp, "/");
printf("%s\n", token[i]);
i++;
while ((token[i] = strtok(NULL, "/")) != NULL) {
    strcat(token[0], token[i]);
    printf("%s", token[i]);
    i++;
}

If my input is 1/2/3/4/5/6 for tmp then the console output would be 13456. The 2 is always missing. Does anyone know how to fix this?

  • 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-25T15:50:12+00:00Added an answer on May 25, 2026 at 3:50 pm

    The two is always missing because on the first iteration of your loop you overwrite it with the call to strcat.

    After entry to the loop your buffer contains: “1\02\03/4/5/6” internal strtok pointer is pointing to “3”. tokens[1] points to “2”.

    You then call strcat: “12\0\03/4/5/6” so your token[i] pointer is pointing to “\0”. The first print prints nothing.

    Subsequent calls are OK because the null characters do not overwrite the input data.

    To fix it you should build up your output string into a second buffer, not the one you are parsing.

    A working(?) version:

    #include <stdio.h>
    #include <string.h>
    int main(void)
    {
        int i = 0;
        char *token[128];
        char tmp[128];
        char removed[128] = {0};
        strcpy(tmp, "1/2/3/4/5/6");
        token[i] = strtok(tmp, "/");
        strcat(removed, token[i]);
        printf("%s\n", token[i]);
        i++;
        while ((token[i] = strtok(NULL, "/")) != NULL) {
            strcat(removed, token[i]);
            printf("%s", token[i]);
            i++;
        }
        return (0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Programming Student here...trying to work on a project but I'm stuck. The project is
I'm trying to work through the problems on projecteuler.net but I keep running into
When trying to work in QtCreator, there is a problem with code completion for
Im trying to work with following code i getting this error at xsltProcessor.importStylesheet Error
I've been trying to work this our for a while, but having trouble. We
trying to work out how to install zipArchive extension with MAMP, but not having
After trying to work on someone else's code, and so many different iterations, I
I´m trying to work with Jzy3D library and paint some charts under openGl. But
I've been trying to get this code to work for hours! All I need
Trying to work with ImageField in django. Here are my models class Album(models.Model): title

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.