Thread: An oversimplified tokenizer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    23

    An oversimplified tokenizer

    Everyone,

    Since I'm not a C expert, please criticize this program. I emailed to a friend fluent in that language. So, he said "Nice!" and told me how optimize the program slightly by replacing a call to the strlen function with the body of the "nonempty" function.

    The program mimics the strtok function without looking for characters that separate tokens.

    Thanks for your thoughts. The program won't seem too amateurish, I hope. I'm fluent in ISO Standard Pascal, though I'd rather write in C instead.

    Code:
    #define MAX 80
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    
     bool token_may_contain(const int c)
    {
        return isalnum(c) || index("-'+", c) != NULL;
    
    }
    
    bool nonempty(const char some_string[])
    {
        return some_string[0] != '\0';
    }
    
    /* Return a token from the line if there's at least one. Otherwise, set the stating topint to -1 */
     const char *a_token_from(const char line[], int * start)
    {
        static char  token[MAX + 1] = "";
        register int here = *start, token_length = 0, line_length = strlen(line);
    
        while (here < line_length && !token_may_contain(line[here]))
            here++;
        while (token_may_contain(line[here]))
            token[token_length++] = line[here++];
        if (nonempty(token))
            token[line_length] = '\0';
        *start = (here < line_length - 1) ? here : -1;
        return token;
    }
    
     void tokenize(const char line[])
    {
        register int counter = 0;
        int start = 0;
        char token[MAX + 1];
    
        while (start >= 0)
        {
           puts(a_token_from(line, &start));
           counter++;
        }
    }
    
    int main(void)
    {
        char line[MAX];
    
       while (fgets(line, MAX, stdin) != NULL)
       {
           puts("Please type the string to tokenize.");
           tokenize(line);    
       }
        
       return 0;
    }
    Last edited by BillMcEnaney; 12-23-2023 at 02:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program tokenizer.
    By Mr.Lnx in forum C Programming
    Replies: 5
    Last Post: 11-28-2013, 02:27 PM
  2. String Tokenizer Help
    By KuuKuu in forum C Programming
    Replies: 5
    Last Post: 01-21-2013, 04:16 PM
  3. C++ String Tokenizer
    By Annorax in forum Game Programming
    Replies: 10
    Last Post: 07-13-2005, 10:41 AM
  4. Tokenizer in C
    By Tarik in forum C Programming
    Replies: 21
    Last Post: 08-26-2004, 06:36 AM
  5. Tokenizer
    By PJYelton in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2003, 03:01 PM

Tags for this Thread