Implementing tokenizer and adding tokens to the linked list

Implementing tokenizer and adding tokens to the linked list

Here we go

#include 
#include 
#include 
struct Node
{
    char * value;
    struct Node *next;
};
struct Node *head = NULL;
struct Node *last = NULL;
char* substr(char*str, int start, int length)
{
    const char* from = str;
    char *to = (char*) malloc(sizeof(str)/sizeof(char));
    strncpy(to, from+start, length);
    return to;
}

void addToLinkedList(char* value)
{
    struct Node *curr_node = malloc(sizeof(struct Node));
    char * newtemp = malloc(strlen(value));
    strncpy(newtemp, value, strlen(value));
    curr_node->value = newtemp;
    curr_node->next = NULL;
    if (head == NULL)
    {
        head = curr_node;
        last = head;
    }
    else
    {
        last->next = curr_node;
        last = curr_node;
    }

}

void printLinkedList()
{
    struct Node * curr_node = head;
    while (curr_node !=NULL)
    {
        printf("%sn", curr_node->value);
        curr_node = curr_node->next;
    }
}
void main()
{
    int delimiter_size = 3, i=0, j=0;
    char* delimit = "abc";
    char* text = "someabccodingabctoabcparseab";
    int length = strlen(text);
    char* temp = (char*)malloc(length);
    while(i < length)
    {
        if (text[i] == delimit[0]) //check if the begin
        {
            //check if the 
            if (strcmp(substr(text, i, delimiter_size), delimit) == 0)
            {
                addToLinkedList(temp);
                i+=delimiter_size;
                j=0;
                memset(temp, '', strlen(temp));
                continue;
            }
        }
        temp[j] = text[i];
        i++;
        j++;
    }
    if (strlen(temp))
    {
        addToLinkedList(temp);
    }
    printLinkedList();
}

See how you would solve these known algorithm problems

Check if there are three numbers a, b, c giving a total T from array A

Find the pairs that makes K Complementary in the given array java solution

testing k complementary pairs algorithm with junit

Java solution for checking anagram strings – tell if phrases are anagrams

Array reversal in Recurrsion

Find longest palindrom from sequence of characters

Finding missing numbers from billion sequential number list file

Check if two strings are anagrams or not

Kadane’s algorithm in C – Dynamic Programming

binary tree problems with solution

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*