[STRING] Convert String To Integer atoi()
16 September, 2013 - 2 min read
Problem: Implement atoi function in C language and give the test cases. atoi function converts a string to an integer. The function prototype is as follows:
int my_atoi(const char str[]);
Solution: The key is to be able to find out the int value of a numeric character.
ASCII value of a numeric character - ASCII value of the character '0' = int value of numeric character.
Ex: '8' - '0' = 8
Code:
#define MIN_INT -2147483648 #define MAX_INT 2147483647 int my_atoi(const char str[]) { if(str == NULL) return 0; int len = strlen(str); if(len <= 0) return 0; int index = 0; //skip leading spaces while(str[index] == ' ') index++; bool isNeg = str[index] == '-'; int outNum = 0; if(isNeg) { index++; // skip white space after the sign while(str[index] == ' ') index++; } while(index < len) { char currentChar = str[index++]; if(currentChar >= '0' && currentChar <= '9') { int oldValue = outNum; int charVal = currentChar - '0'; outNum *= 10; outNum += charVal; //overflow underflow detection if(outNum < oldValue) { if(isNeg) outNum = MIN_INT; else outNum = MAX_INT; return outNum; } } else break; } if(isNeg) outNum = outNum * -1; return outNum; }
atoi Test Cases:
Input : Output "" : 0 "0" : 0 "1" : 1 "-1" : -1 "10" : 10 "-10" : -10 "1234567890" : 1234567890 "23 45" : 23 " 99" : 99 " -66" : -66 "- 77" : -77 "55 " : 55 "-2147483648" : -2147483648 (MIN) "2147483647" : 2147483647 (MAX) "2147483648" : 2147483647 (overflow) "-2147483649" : -2147483648 (underflow) "abc*" : 0 "23ab" : 23 "23ab34" : 23 "b31" : 0
END