DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Problem with strtok function when tokenizing user input

  1. #1
    Join Date
    Nov 2006
    Orlando, Florida

    Smile Problem with strtok function when tokenizing user input

    Hi, everyone. I am new to this forum, and I hope that through time, I will be able to help out this community in any way I can.

    First, though, I have a quick question pertaining to C language, particularly the strtok function. I am creating a program to differentiate a user-defined equation and return the derivative to the main function (or print it out in the void function). I am permitting the user (for now) only to enter polynomial equations of the second degree (eg x^2 +2x +2). I created a function that is supposed to split up the equation initially by using the 'space' character as the delimiter. to test, i tried to print out each token as it was parsed by strtok. However, after the first item has been tokenized, the second and third characters are returned from the strtok function as (null). Here is an example:

    Please enter your equation: x^2 +2x +2

    token1 = x^2
    token2 = (null)
    token3 = (null)

    How do I get the strtok function to recognize subsequent values after x^2? Here is the code for the derivative function i am using:

    void derive(){
    char equation[50];
    const char delim1[] = " ", delim2[] = "x ^";
    char derivative[50];
    char *tok1, *tok2, *tok3;

    printf("Please enter your equation: ");
    scanf("%s", &equation);

    tok1 = strtok(equation, delim1);
    printf("token1 = %s\n", tok1);
    tok2 = strtok(NULL, delim1);
    printf("token2 = %s\n", tok2);
    tok3 = strtok(NULL, delim1);
    printf("token3 = %s\n", tok3);


    system("PAUSE"); //this is for testing purposes, not in final version


  2. #2
    Join Date
    Dec 2003
    you need to grab the position of the space and pass the rest of the string to strtok, not NULL. Passing null to the function instead of a string means it got an empty string, which wont really do much.

Similar Threads

  1. call function for button
    By angela_quests in forum VB Classic
    Replies: 2
    Last Post: 04-13-2007, 04:57 AM
  2. Replies: 2
    Last Post: 04-18-2005, 12:01 PM
  3. Replies: 0
    Last Post: 07-20-2002, 10:12 PM
  4. I am helpless - Edit Info form
    By Dan in forum VB Classic
    Replies: 0
    Last Post: 03-17-2000, 06:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center