Implement strtok() function. (It tokenizes the string )

Implementation of strtok

char* strtok(char *s, const char *delim) {
static char *old = NULL;
char *token;
if(! s) {
s = old;
if(! s) {
return NULL;
}
}
if(s) {
s += strspn(s, delim);
if(*s == 0) {
old = NULL;
return NULL;
}
}

token = s;
s = strpbrk(s, delim);
if(s == NULL) {
old = NULL;
} else {
*s = 0;
old = s + 1;
}
return token;
}

Write implementation of memcpy and memmove functions? What is the difference between the two?

Implementation of memcpy

void memcpy(void* dest, void* src, int n_size) {
char* d = (char*) dest;
char* s = (char*) src;
while(n_size– > 0) {
*d++ = *s++;
}
}

Implementation of memmove

void memmove(void* dest, void* src, int n_size) {
char* d = (char*) dest;
char* s = (char*) src;
if(d > s) {
while(n_size– > 0) {
*d– = *s–;
}
} else {
while(n_size– > 0) {
*d++ = *s++;
}
}
}

memmove ensure correct copying when the two buffers overlap.

What is a double pointer? Why is it required? Is it necessary in C++?

Pointer to a pointer is called double pointer. It is needed when one wants to manipulate the pointer itself like in linked list manipulation etc. In C++ same thing can be accomplished using reference which also avoids the overhead of copying.

Technology