ATOI- Ascii to Integer, Write implementation by looking the specification on MSDN
http://msdn2.microsoft.com/en-us/library/hc25t012(vs.71).aspx
Comments: Flat Nested Threaded Embedded Oldest First Newest First 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
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
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]=='-')
{
bIsNegative=true;
}
c=*theString;
while(c>='0' && c<='9')
{ num*=10; num+=(c-'0'); }
if(bIsNegative) return -num; return num;
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);}
int atoi(const char *str){ if (!str) return 0;
while (*str == ' ' || *str == '\t') str++;
while (*str >= '0' && *str <= '9') { ret = ret * 10 + *str - '0'; str++; } return (neg ? -ret : ret);}
// 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);}