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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:14:33+00:00 2026-05-26T18:14:33+00:00

I need to use strtok to read in a first and last name and

  • 0

I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays?

#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="test string.";
  char * test;
  test = strtok (str," ");
  while (test != NULL)
  {
    printf ("%s\n",test);
    test= strtok (NULL, " ");
  }
  return 0;
}
  • 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-26T18:14:34+00:00Added an answer on May 26, 2026 at 6:14 pm

    Here is my take at a reasonably simple tokenize helper that

    • stores results in a dynamically growing array
    • null-terminating the array
    • keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a literal char[], at least I think in C99)

    To make the code re-entrant, use the non-standard strtok_r

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    char** tokenize(const char* input)
    {
        char* str = strdup(input);
        int count = 0;
        int capacity = 10;
        char** result = malloc(capacity*sizeof(*result));
    
        char* tok=strtok(str," "); 
    
        while(1)
        {
            if (count >= capacity)
                result = realloc(result, (capacity*=2)*sizeof(*result));
    
            result[count++] = tok? strdup(tok) : tok;
    
            if (!tok) break;
    
            tok=strtok(NULL," ");
        } 
    
        free(str);
        return result;
    }
    
    int main ()
    {
        char** tokens = tokenize("test string.");
    
        char** it;
        for(it=tokens; it && *it; ++it)
        {
            printf("%s\n", *it);
            free(*it);
        }
    
        free(tokens);
        return 0;
    }
    

    Here is a strtok-free reimplementation of that (uses strpbrk instead):

    char** tokenize(const char* str)
    {
        int count = 0;
        int capacity = 10;
        char** result = malloc(capacity*sizeof(*result));
    
        const char* e=str;
    
        if (e) do 
        {
            const char* s=e;
            e=strpbrk(s," ");
    
            if (count >= capacity)
                result = realloc(result, (capacity*=2)*sizeof(*result));
    
            result[count++] = e? strndup(s, e-s) : strdup(s);
        } while (e && *(++e));
    
        if (count >= capacity)
            result = realloc(result, (capacity+=1)*sizeof(*result));
        result[count++] = 0;
    
        return result;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I need use this event so I can navigate trought blog posts. I
I need use a progress bar with the pbstMarquee style, I read this question
I have two tables (Master, Responses) I need use the Master_Id field from the
I need use the same view for three different actions in controller. How can
I need use this framework with IOS 4.3 Scheme, How can i do this?
How I can add proxy/socks4/socks5 to C# Socket. I need use it throw Socket.
I have a string AAbbCC what I need is to copy the first two
I need to use the C library in a C# project. How can I
In my project I need use custom Dialog instead AlertDialog. But I have two
I need use this method with three distinct classes: Orders, Customers, Suppliers public void

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.