About Me

My photo
Mostly software programming related blogs.

Thursday, April 30, 2015

strtok in C/C++ Program - Code implementation - string tokenizer

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

using namespace std;

char* my_strtok(char *inp, char *delim);

main()
{
        char src[128] = {'\0'};
        char delim[16] = {'\0'};

        char *out;

        cout << "enter input string src: ";
        gets(src);
        cout << "enter input string dilimiter: ";
        gets(delim);

        cout << "src is " << src << endl;
        cout << "delimiter is " << delim << endl;

        out  = my_strtok(src, delim);  

        while(out != NULL) {
                cout << "token is " << out << endl;
                out = my_strtok(NULL, delim);
        }  
}

char *my_strtok(char *inp, char *deli) {

        static char *start = NULL;
        char *ch = NULL, *delim = NULL, *ret = NULL;
        uint32_t num_chars = 0;

        if (inp != NULL) {
                start = inp;
        }  

        if (start == NULL || deli == NULL) {
                return NULL;
        }  

        ch = ret = start;

        while (*ch != '\0') {
                delim = deli;
                while(*delim != '\0') {
                        if (*ch == *delim) {
                                *ch = '\0';
                                if (num_chars > 0) {
                                        start = ch+1;
                                        return ret;
                                } else {
                                        ret = ch+1;
                                        break;
                                }
                        }
                        delim++;
                }
                ch++;
                num_chars++;
        }
        if (*ch == '\0') {
                start = NULL;
                if (inp == NULL && strlen(ret) != 0) {
                        return ret;
                }
        }
        return NULL;
}

No comments: