Visit our sponsor Granny's Eggs  
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


Write and test ATOI function

ATOI- Ascii to Integer, Write implementation by looking the specification on MSDN

http://msdn2.microsoft.com/en-us/library/hc25t012(vs.71).aspx


 

Comments:

Implementation of ATOI
By eMSFTAdmin on Thursday, March 22, 2007 (UMST)

int atoint(const char* ch)
{
 int number = 0;
 int *nPtr = &number;
 bool bIsNegative = false;
 
 if (ch == NULL)
  return 0;
 else if (*ch == 43)
  *ch++;
 else if(*ch == 45)
 {
  bIsNegative = true;
  *ch++;
 } 

 while (*ch != '\0' && *ch != ' ' && *ch >= 48 && *ch <= 57)
 {
  *nPtr = (*nPtr*10);
  *nPtr +=(*ch-48);
  *ch++;
 }
 
 if (bIsNegative)
  number *= -1;
 
 return  number;
}

 

 

 

TEST CASES:

Functional Testing:
1. Pass a string containing negative sign to the function, it should return the converted negative number.
2. Pass a number with a + sign in front of it, it should return a positive number back.
3. Pass a floating point number to the function, it should return the numbers till the decimal point .
4. Pass a string with alphabets in it, it should return the number till the pont where alphabets end.
5. Pass a big positive number, it should return the numeric equivalent of a number.
6. Pass a string containing all characters to the function, it shuold return 0;
7. Pass a string with special characters in the string like "*" or "," and ".". It should convert number only till the special character.

Boundary conditions check:
1. Pass an empty string to the function, it should return 0;
2. Pass 0 to the function, it should return a numeric 0.
3. Pass NULL to the string and the function should return 0.
4. Pass a huge positive number that exceeds integer boundary bounds, it should return the wrapped around number.
5. Pass a decimal point number starting with 0 like 0.0001 and it should return 0.

Localization: N/A

Security Testing:
1. Check the Input string passed to the function it should not be modified.
2. When multiple threads are invoked on high load neither the input nor the output should be garbled.

Performance Testing:
1. The function must be tested by invoking it using multiple threads and the response time must be observed.
The response time should be almost linear.
2. The function must be tested by invoking it using multiple threads and Memory usage should be observed, it should not occupy
large chunk of memory.

Load Testing:
1. Test the function with high number of threads, response must be correct all the time.
2. Test the function with a high number of threads and it should not bring down the application.

Stress Testing:
1. Test the function using low amount of resources i.e. available memory and CPU time. Its performance should not degrade drastically.

Stability Test:
1. After every call, the function should release all the resources occupied by it.

Reply to this Comment
 

obs
By zoltar on Tuesday, August 28, 2007 (UMST)
why *ch++ ? I think ch++ is enough to go to next char. Why using *nPtr = &number; It is necessary to use the pointer here? we can use only number variable ;)

Reply to this Comment
 

elegant solution
By jagatsastry on Thursday, August 30, 2007 (UMST)
int atoi(const char* ch) { char*theString=ch; bool bIsNegative=false; int num=0; char c; if(theString==NULL) return 0; if(theString[0]=='+') theString++; else if(theString[0]=='-') { theString++; bIsNegative=true; } c=*theString; while(c>='0' && c<='9') { num*=10; num+=(c-'0'); } if(bIsNegative) return -num; return num; }

Reply to this Comment
 

@msAdmin
By jagatsastry on Thursday, August 30, 2007 (UMST)
why are the line endings not being retained?

Reply to this Comment
 

Press the 10101 Botton from the toolbar to add code with lines
By eMSFTAdmin on Thursday, August 30, 2007 (UMST)
int atoi(const char* ch) 
{ char*theString=ch;
 bool bIsNegative=false;
 int num=0;
 char c;
 if(theString==NULL) 
	return 0;
 if(theString[0]=='+') 
	theString++;
 else 
	if(theString[0]=='-') 
	{ 
		theString++; 
		bIsNegative=true; 
	} 
c=*theString; 
while(c>='0' && c<='9') 
{ num*=10; num+=(c-'0'); }
 if(bIsNegative) return -num; return num; 
} 

Reply to this Comment
 

Gotta ignore leading spaces and tabs.
By christophilus on Sunday, October 28, 2007 (UMST)

int ATOI(const char *str)
{
   if (!str) return 0;

   while ((*str == ' ' || *str == '\t') && *str) str++;

   int  ret = 0;
   bool neg = (*str == '-');
 
   if (neg || *str == '+') str++;

   while (*str && *str >= '0' && *str <= '9')
   {
      ret = ret * 10 + *str - '0';
      str++;
   }
 
   return (neg ? -ret : ret);
}

Reply to this Comment
 

Er... That should look like this.
By christophilus on Sunday, October 28, 2007 (UMST)

int atoi(const char *str)
{
   if (!str) return 0;

   while (*str == ' ' || *str == '\t') str++;

   int  ret = 0;
   bool neg = (*str == '-');
 
   if (neg || *str == '+') str++;

   while (*str >= '0' && *str <= '9')
   {
      ret = ret * 10 + *str - '0';
      str++;
   }
 
   return (neg ? -ret : ret);
}

Reply to this Comment
 

One more thing...
By christophilus on Sunday, October 28, 2007 (UMST)

// Got the while statement down to one line...

int atoi(const char *str)
{
   if (!str) return 0;

   while (*str == ' ' || *str == '\t') str++;

   int  ret = 0;
   bool neg = (*str == '-');
 
   if (neg || *str == '+') str++;

   while (*str >= '0' && *str <= '9')
   {
      ret = ret * 10 + *str++ - '0';
   }
 
   return (neg ? -ret : ret);
}

Reply to this Comment
 

Add Your Comment

New Articles
  • Phone screen from MICROSOFT Denmark for SDET
    Technical Phone screen after Escreen

  • Please post some non-tech qs for User Experience and Business Analyst job

    I am a CS Grad applied in non-tech position.Please help!


  • 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'.


  •  

    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

  • male enhancment buy viagra tramadol online
    Posted by SurojattSit on 2008年9月30日 (UMST)

  • penis enlargement viagra cheap tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)

  • penis viagra online Tramadol
    Posted by BobeCrobChext on 2008年9月30日 (UMST)



  •