Microsoft Interview Question
Software Engineer in Testschar *mystrtok(char *str,char *deli)
{
static char *s;
static int pos;
int j,start=pos;
if(str!=NULL)
s=str;
while(s[pos]!='\0')
{
j=0;
while(deli[j]!='\0')
{
if(s[pos]==deli[j])
{
s[pos]='\0';
pos=pos+1;
if(s[start]!='\0')
return &s[start];
else
{
start=pos;
pos--;
break;
}
}
j++;
}
pos++;
}
s[pos]='\0';
if(s[start]=='\0')
return NULL;
else
return &s[start];
}
char* lastPos = 0;
char token[100];
char* mystrtok(char* str, char* delim)
{
if(!str && !lastPos)
return 0;
if(!delim)
return 0;
if(str)
lastPos = str;
int delim_len = strlen(delim);
char* strt_ptr = lastPos;
int count = 0;
while(*lastPos != '\0')
{
bool is_found = false;
for(int y=0; y<delim_len; y++)
{
if(*(delim + y) == *lastPos)
is_found = true;
}
lastPos++;
if(is_found)
break;
count++;
}
if(*lastPos == '\0')
lastPos = 0;
//added to remove empty ones
if(!count)
return mystrtok(0, delim);
//
for(int x=0; x<count; x++)
token[x] = *(strt_ptr + x);
token[count] = '\0';
return token;
}
char *mstrtok(const char *str,const char *del)
{
static char *sp = NULL ;
static int pos = 0;
//this is the first time the function is getting called
if (sp == NULL)
{
if ((str == NULL) ||(del == NULL))
return NULL;
sp = new char[mstrlen(str)];
strcpy(sp,str);
}
//we have reached the end of string
if (sp[pos] == '\0')
return NULL;
int start = pos;
//find the delimiter
while (sp[pos]!='\0')
{
bool found = false;
for ( int i = 0 ; i < mstrlen(del);i++)
{
if (sp[pos]==del[i])
{
found = true;
break;
}
}
if (found)
{
sp[pos]='\0';
pos++;
return &sp[start];
}
pos++;
}
return &sp[start];
}
#include<stdio.h>
#include<stdlib.h>
char * strtok(char * s, char *comp);
void main()
{
char s[100], *p, delimit[20];
int i=0, len=0;
printf("Enter the input string \n");
gets(s);
printf("Enter the delimiter string \n");
gets(delimit);
while(len++ != '\0');
p = strtok(s,delimit);
while(p != NULL)
{
printf("%s \n", p);
p = strtok(NULL, delimit);
}
}
char * strtok(char * str, char *comp)
{
static int pos;
static char *s;
int i =0, start = pos;
// Copying the string for further calls of strtok
if(str!=NULL)
s = str;
i = 0;
int j = 0;
//While not end of string
while(s[pos] != '\0')
{
j = 0;
//Comparing of one of the delimiter matches the character in the string
while(comp[j] != '\0')
{
//Pos point to the next location in the string that we have to read
if(s[pos] == comp[j])
{
//Replace the delimter by \0 to break the string
s[pos] = '\0';
pos = pos+1;
//Checking for the case where there is no relevant string before the delimeter.
//start specifies the location from where we have to start reading the next character
if(s[start] != '\0')
return (&s[start]);
else
{
// Move to the next string after the delimiter
start = pos;
// Decrementing as it will be incremented at the end of the while loop
pos--;
break;
}
}
j++;
}
pos++;
}//End of Outer while
s[pos] = '\0';
if(s[start] == '\0')
return NULL;
else
return &s[start];
}
/*
* Strtok implementation
*/
char * mystrtok(char *str, char *tok)
{
static char buffer[100];
static int pos =0;
char *trav;
int i =0;
if(str == NULL)
{
return NULL;
}
if(pos != 0) {
pos = pos+1;
}
trav = str+pos;
while(*trav)
{
if(*trav != *tok)
{
buffer[i++] = *trav++;
pos++;
}
else
{
break;
}
}
buffer[i] = '\0';
return(&buffer[0]);
}
main()
{
char ch[] = "Hi I AM Here";
char *tok;
char *delimiter = " ";
tok = mystrtok(ch,delimiter);
while(*tok)
{
printf("\n%s\n",tok);
tok = mystrtok(ch,delimiter);
}
}
/*
* Strtok implementation
*/
char * mystrtok(char *str, char *tok)
{
static char buffer[100];
static int pos =0;
char *trav;
int i =0;
if(str == NULL)
{
return NULL;
}
if(pos != 0) {
pos = pos+1;
}
trav = str+pos;
while(*trav)
{
if(*trav != *tok)
{
buffer[i++] = *trav++;
pos++;
}
else
{
break;
}
}
buffer[i] = '\0';
return(&buffer[0]);
}
main()
{
char ch[] = "Hi I AM Here";
char *tok;
char *delimiter = " ";
tok = mystrtok(ch,delimiter);
while(*tok)
{
printf("\n%s\n",tok);
tok = mystrtok(ch,delimiter);
}
}
Here is my code...
- Gaurav April 04, 2008