Microsoft Interview Question for Software Engineer in Tests






Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code...

#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];
}

- Gaurav April 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice

- Sagar March 25, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char *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];
}

- rajnesh November 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is a problem if we can this function twice, the static point need to be reset

- Anonymous July 13, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;

}

- Anonymous September 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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];
}

- MJ September 18, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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];

}

- Anonymous January 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* 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);
    }
}

- Anonymous January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/*
* 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);
    }
}

- Anonymous January 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

asdf

- Anonymous December 21, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More