Thread: string tokens

  1. #1
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92

    string tokens

    I've tried to extract numbers, letters and punctuations etc, from strings but I've never succeeded. For example, how would you 'extract' the words/numbers in; text<space>text<space>number or number<punctuation>number and so on. Can you use strtok() to do that? I don't have any test code, sorry, and I don't even know how to approach this. I looked in the FAQ, but there's only examples created in C++, which I don't know. A push in the right direction would be appreciated.

  2. #2
    Registered User Markallen85's Avatar
    Join Date
    Nov 2002
    Posts
    53
    this is some code I wrote a while ago to turn a csv line into html, using strtok():

    Code:
          token=strtok(templine,",");
          for(m=0;token!=NULL;m++)
            {
            fprintf(Ohtml,"<td>");
            fprintf(Ohtml,"%s",token);
            fprintf(Ohtml,"</td>");
            token=strtok(NULL,",");
            }
    basically, call strtok with the string (templine), and the separating character. this retruns the first token. every time you want the next one, call strtok with a NULL string.

    hope that helps
    -mark
    "never argue with an idiot, they will drag you down to their level and beat you with experience"

  3. #3
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Thank you, Markallen85, that gave me some idea what I need to do.

    To be more specific, this is the problem I have: <number><operator><number>... where number can be any number and opeartor is either +, -, / or *. I want to separate the number and the opeartors, and this is the code I have so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define BUFFER 1024
    
    int main() {
    	
    	int loop = 1, len, i;
    	char seq[BUFFER];
    	char *token;
    	
    	while(loop == 1) {
    	
    		printf(": ");
    		/* gets the line */
    		fgets(seq, sizeof(seq), stdin);
    		/* gets the length of the string */
    		len = strlen(seq);
    		
    		token = strtok(seq,"+");
    		
    		for(i=0; token != NULL; ++i) {
    			printf("%s",token);
    			token=strtok(NULL,",");
    		}
    ...
    Some questions: what type should 'token' be (I use both chars and integers/floats)? Does it have to be a pointer? Is it possble to pre-define the operators? I tried with #define OPS + - / * and put OPS where the "+" is now. I got errors like: error: parse error before ')' token

    I'm confused.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I don't think you want to strtok for this. Write a function called lex that returns tokens. A token you can define like this
    Code:
    #define TOK_PLUS 0
    #define TOK_SUB 1
    #define TOK_DIV 2
    #define TOK_MULT 3
    #define TOK_EOF 4
    
    typedef struct Token {
           int type;
           int value;
    } Token;

    The lex routine will skip space, read the token, and then return the token. In order to do this, look up getc, isspace, isdigit, and isalpha which are in ctype.h and stdio.h.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM