Thread: Strtok Problem

  1. #1
    Registered User
    Join Date
    Sep 2012

    Strtok Problem

    hello everyone, im new in here and im having a headache with my program,the thing is that i need to get a input from the keyboard and then separate it using strtok but have to separate the tokens using 4 diferent cases and in each case i need to print the result and save it to a string like this:

    input String : Label Instruction #50,Y; Label <with>

    and the output should look like this:

    Label: Label
    Instruction: Instruction
    Character 1: #50
    Character 2: Y
    Comentaries: Label <with>

    also it has to be able to reconize if a instruction is missed like this:

    Input String: adda

    Output String
    Label: -----
    Instruction: adda
    Character 1: -----
    Comentaries: -----

    My code can accept the first and correct instruction but when i type a incorrect one like in the second input it ignores it and continue like the first atempt just ading <null> sometimes,i have tryed to use if to be able to separate each token with its delimeter but everitime i compile it it ignores the if statement no matter what argument i gave it i dont know what else to do
    Heres my code

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    #include <ctype.h>
    int main() {     char word[256];
    char *token;
    while (token != NULL)
                char delimiter[]="\n , ;";
                token=strtok (word,delimiter);
                //if(token != "\n")
                //char delimiter[]="\n , ;";
                //token=strtok (word,delimiter);
                //if (delimiter != " "||"\t" || "_")
                printf("Label \"%s\"\n", token);
                token = strtok (NULL, "\n , ;");   //(NULL, "_,.-")
                //token=strtok (word,delimiter);
                //printf("Label ----------\n");
                //if (delimiter != "\n"||"\t")
                printf("Instruction \"%s\"\n", token);
                token = strtok (NULL, "\n , ;");   //(NULL, "_,.-")
                printf("Character \"%s\"\n", token);
                token = strtok (NULL, "\n , ;");   //(NULL, "_,.-")
                printf("Character 2 \"%s\"\n", token);
                token = strtok (NULL, "\n , ;");   //(NULL, "_,.-")
                printf("Comentaries \"%s\"\n",token);
                token = strtok (NULL, ";");
    //            printf("Character 2\"%s\"\n", token);
     //           token = strtok (NULL, "\n , ;");   //(NULL, "_,.-")
    token = NULL;
                //token = NULL;
                //printf("Comentaries \"%s\"\n", token);
                //token = NULL;
    the // coments are all my failed attempts to try to make it work =(
    Can someone help me please?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    United States
    Ideally you would want to break up the string like this.

    Space first:
    "Label Instruction #50,Y; Label <with>"
    You get Label
    Space second using NULL for the first argument:
    You get Instruction
    "," third, using NULL...:
    You get #50
    ";" fourth, using NULL...:
    You get Y
    And the residue is Label <with>.

    So you will have to use different delimiter strings as you process the input just like I did.

    This will not necessarily help you with incorrect input. If the string is outright wrong -- like "adda" -- there is nothing to say that it belongs in the instruction field. The only way to do it right is to follow the format.

    " adda,;"
    Should parse to
    Label: ""
    Instruction: adda
    The rest would be empty strings as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strtok problem
    By erasm in forum C Programming
    Replies: 2
    Last Post: 09-01-2009, 07:58 AM
  2. Strtok Problem.
    By sergioms in forum C Programming
    Replies: 8
    Last Post: 11-23-2008, 12:03 PM
  3. Strtok problem
    By vin_pll in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2008, 02:57 PM
  4. strtok problem again
    By cstudent in forum C Programming
    Replies: 6
    Last Post: 04-24-2008, 02:39 AM
  5. strtok problem
    By cnu_sree in forum C Programming
    Replies: 6
    Last Post: 11-07-2007, 02:41 AM