Home > Back-end >  How to Tokenize String without using strtok()
How to Tokenize String without using strtok()

Time:09-22

I'm trying to tokenize a string without using a strtok().

When I run characters of string, it will print in each line.

For instance, when I run:

printfTokens("Hello from other side!");

The output should be:

Hello
from 
other
side!

As I'm just learning C, I'm stuck for hours on how to implement this program. So far, I only know the basics and playing around with not (still haven't learned any calloc, malloc, etc).

So far I have this code, but the output does not print anything.

#include <stdio.h>
#include <string.h>

#define MAX_WORD 100

void printfTokens(char *inputString) {

    int i;

    /*int inputStringLength;

    for(i = 0; inputString[i] != '/0'; i  ) {
        inputStringLength  ;
    }*/

    while(inputString[i] != '\0') {
        char testing[MAX_WORD];
        while(inputString[i] != ' ') {
            testing[inputString[i]]  ;
            i  ;
        }
        printf("%s", testing);
        i  ;
    }
}

int main() {

    printfTokens("TESTING ONE! TWO! THREE!");

    return 0;
}

CodePudding user response:

  1. You do not initialize the variable i.

  2. while(inputString[i] != '\0') can be written while(inputString[i]).

  3. testing[inputString[i]] makes sense to count the number of occurrences of a given character from inputString, but it does not make sense to print it. You may want to do something like:

    while(1) 
    {
     char testing[MAX_WORD], *t=testing;
     while(inputString[i]&&(inputString[i]!=' '))
         *t  =inputString[i  ];
     if (t>testing) printf("%s", testing);
     if (!inputString[i]) break;
     i  ;
    }
    
  4. It would be better to name MAX_WORD_LENGTH instead of MAX_WORD.

These are a few problems in your code.

CodePudding user response:

Sample tokenization function.

size_t tokenize(const char *inputString, const char *delim, char **argv, size_t maxtokens) 
{
    size_t ntokens = 0;
    char *tokenized = strdup(inputString);
    if(tokenized)
    {
        argv[0] = tokenized;
        while(*tokenized)
        {
            if(strchr(delim, *tokenized))
            {
                *tokenized = 0;
                ntokens  ;
                if(ntokens == maxtokens - 1) 
                {
                    break;
                }
                argv[ntokens] = tokenized   1;
            }
            tokenized  ;
        }
    }
    return ntokens   1;
}

int main() 
{
    char *tokens[10];
    size_t ntokens = tokenize("TESTING ONE! TWO! THREE!", " ", tokens , 10);
    for(size_t i = 0; i < ntokens; i  )
    {
        printf("Token[%zu] = `%s`\n", i, tokens[i]);
    }
    free(tokens[0]);
    return 0;
}

https://godbolt.org/z/znv8PszG6

  • Related