Visit our sponsor Five Planet Juices  
Home
Microsoft Interview Process
Microsoft HR Questions
Technical Questions
Puzzles/Riddles
Resume Tips and Template
Discuss
Question to Interviewer
Interview Tips
Term Of Use
Site Feedback


Rewrite the StrTok() function
Do as many optimization as you can.

 

Comments:

Here is a c++ solution...
By smalandren on Thursday, March 20, 2008 (UMST)

I am new this site and this is my first post. I have found this website very interested, now just to prepare for an interview but also to have fun with some of the questions.

 

My solution is below. It runs in O(2n), where n is the size of the input string.

 

void StrTok(const char *str, char **&tokens, char delim)
{
   char *strptr = (char *)str;
   char *ptrstart=strptr;
  
   int strlen=0;
   int ntokens=1;
   int tokendx=0;
  
   if( !str )
   {
      tokens = NULL;
      return;
   }
   //Find # of tokens that will be created;
   while(*strptr)
   {
      if(*strptr == delim)
      {
         ntokens++;
         //Don't count consecutive delimeters
         while(*strptr++ == delim);
         continue;
      }
      strptr++;
   }

   //Now declarte the ntokens array of char*
   tokens = new char* [ntokens+1];
   strptr = (char *)str;
   do
   {
      if(*strptr == delim || !(*strptr))
      {
         strlen = strptr-ptrstart;
         *(tokens+ tokendx) = new char[strlen+1];
         memcpy( *(tokens+ tokendx), ptrstart, strlen);
         (*(tokens+ tokendx++))[strlen]='\0';
         ptrstart = strptr+1;

         //Don't count consecutive delimeters
         while(*strptr++ == delim);
         continue;
      }
   }while(*strptr++);

   *(tokens+ tokendx)=NULL;
}

 

I hope the code is clear enough

Reply to this Comment
Average Rating:

well this is another question...
By smalandren on Thursday, March 20, 2008 (UMST)

Actually, I misread the question. I thought it was about writing a strtok funciton, but I think it is to rewrite the exisitng standard C one.

Maybe if I have more time I will we write it.

Reply to this Comment
Average Rating:

Add Your Comment

New Articles
  • Combinations in a character array.
    Write a program that takes input a char array and outputs all the combinations of the characters in the character array.
    Example: consier char array {'a','b','c'}
    the output shouold be abc,cab,bac,acb,cba,bca that is all the combinations of characters 'a','b','c'.


  • N-Queen Problem.
    Write a Program to solve N-Queen Problem.

  • Reverse a string
    Reverse a string using recursive function

  •  

    Most Popular Articles
  • Crazy Guy On The Airplane
    A line of 100 airline passengers is waiting to board a plane. they each hold a ticket to one of the 100 seats on that flight.

  • What are your greatest weaknesses
    Beware - this is an eliminator question, designed to shorten the candidate list. Any admission of a weakness or fault will earn you an “A” for honesty, but an “F” for the interview.

  • Question: Tell me about yourself.
    Many candidates, unprepared for the question, skewer themselves by rambling, recapping their life story, delving into ancient work history or personal matters.

  •  

    New Posts

  • dfasdf sd fsdf adult
    Posted by neteGaftFarse on Monday, July 07, 2008 (UMST)

  • King went already passed were understand offender.
    Posted by Kixhwwld on Monday, July 07, 2008 (UMST)

  • Another struck rapier himself troubled police crossbones.
    Posted by Gdpakshw on Sunday, July 06, 2008 (UMST)



  •