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();
}