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

  • Home
  • SEARCH
  • 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 8219619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:14:06+00:00 2026-06-07T13:14:06+00:00

Here i have one string means character array. this array contains separator character also.

  • 0

Here i have one string means character array. this array contains separator character also. Now i want to take out only strings without this separator.

I am using strtok function.

I want to separate all my words with this separator.

But here i am getting some problems in this code.

Please Let me know if i am doing something wrong or tell me different way simple and easy way to achieve this thing,consider memory risk also.

Here this line pch = strtok (str,&sep); get nothing. Problem is here only.

Code

/* strtok example */
#include <stdio.h>
#include <string.h>

#define SEPRATOR 0x03

int main ()
{
    char str[1024];
    int count = 0;
    char sep;
    memset(str,0,1024);
    sep = SEPRATOR;

    memcpy(str,"1",strlen("1"));
    count = count + strlen("1");
    str[count++] = sep;
    memcpy(str+count,"0",strlen("0"));
    count = count + strlen("0");
    str[count++] = sep;
    memcpy(str+count,"2",strlen("2"));
    count = count + strlen("2");
    str[count++] = sep;
    memcpy(str+count,"abc_1.0.xyz",strlen("abc_1.0.xyz"));
    count = count + strlen("abc_1.0.xyz");
    str[count++] = sep;
    memcpy(str+count,"3",strlen("3"));
    count = count + strlen("3");
    str[count++] = sep;
    memcpy(str+count,"23455.456",strlen("23455.456"));
    count = count + strlen("23455.456");

    printf("Input String = %s\n",str);

    char * pch;
    int pchcount = 0;
    pch = strtok (str,&sep);
    while (pch != NULL)
    {
        pchcount++;
        printf ("%d === %s\n",pchcount,pch);
        pch = strtok (NULL,&sep);
    }
    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-06-07T13:14:07+00:00Added an answer on June 7, 2026 at 1:14 pm

    The second argument to strtok() is a null terminated string (char*), but you passing the address of a single char. While a char*, it will not be null terminated. Change to:

    char sep[2] = { SEPRATOR, 0 };
    

    Changing current uses of sep accordingly:

    str[count++] = sep[0];
    
    pch = strtok(str, sep);
    

    You may find snprintf() simpler to construct your str than the sequence of memcpy() invocations.

    Also, if you want to initialise str:

    char str[1024] = ""; /* instead of performing a memset(). */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the scenario, I have a WCF service call that takes one string
Here is my situation: I have one table that contains a list of drugs
Here i want to do UDP Socket programming in C. I have one device
Ok, so here's what I want to do, preferably with jQuery 1.4.2: Have one
I have a big txt file ,with many strings ,say string-I-want-to-change,now I need to
Here is the situation: You have one long-running calculation running in a background thread.
I have one problem with parsing *.docx document with OpenXML (C#). So, here's my
so here's the problem I have one form, it outputs search results after submit,
here is the problem. I have one PC using VS2008 (SP1 ) and another
I have created one UIImageView Here's code capView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:kARROW_CAP_NAME]]; capView.frame

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.