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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:33:46+00:00 2026-05-28T23:33:46+00:00

This should tokenize space delimited fields in the string str: float values[2*linesnum(str, length)]; char

  • 0

This should tokenize space delimited fields in the string str:

float values[2*linesnum(str, length)];
char * pch;
pch = strtok(str, " ");
while (pch != NULL) {
    printf("%s\n", pch);
    pch = strtok(NULL, " ");
}

linesnum() just counts the number of ‘\n’ characters in a string. The above code correctly prints all values from str that I expect.

However, this prints only the first value, and then every second value thereafter:

int k = 0;
float values[2*linesnum(data, length)];
char * pch;
pch = strtok(data, " ");
while (pch != NULL) {
    values[k] = atof(pch);
    //printf("%s\n", pch);
    printf("%f\n", values[k]);
    pch = strtok(NULL, " ");
    k++;
}

Here is an example of what the input string looks like:

3.31 2.16
4.28 0.56
7.20 3.09
11.18 5.06

In the first code, it will print out:

3.31
2.16
4.28
0.56
7.20
3.09
11.18
5.06

In the second code, it will print out:

3.310000
2.160000
0.560000
3.090000
5.060000

I must be doing something silly. Is it putting only half of the values into the array? Why, if all the values show up when I directly print the string pch? It is as if the single command pch = strtok(NULL, " "); is producing two output, and I can capture only one.

  • 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-28T23:33:47+00:00Added an answer on May 28, 2026 at 11:33 pm

    The problem appears to be that your string looks something like this: "1.234 5.678\n9.012 3.456", that is, the second and third numbers (and the fourth and fifth numbers, and so on) are separated by a '\n' character, instead of a ' ' character. To remedy this, you can just add '\n' to your string of delimiter characters:

    pch = strtok(NULL, " \n");
    

    Just a note: the reason your original code appears to work is that the second value read is actually (in my example) "5.678\n9.012", which, when printed, looks like

    5.678
    9.012
    

    Even though it looks like two values, in reality it is only one string.

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

Sidebar

Related Questions

I'm trying to adapt this answer How do I tokenize a string in C++?
I am using a simple program to tokenize a string using strtok function. Here
This should be fine seeing as the CLR hasn't actually changed? The boxes running
This should hopefully be a simple one. I would like to add an extension
This should a quick question for some easy rep. I'm doing some PHP Website
This should be a really really simple thing, but for some reason it is
This should be easy for many of you, but for me it's just another
This should be easy, but I'm not sure how to best go about it.
This should be straight forward for a guru. I don't have any code really
This should be a simple question, but I haven't been able to find a

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.