Interview Questions

239. create your own atoi()?

Microsoft Interview Questions and Answers


(Continued from previous question...)

239. create your own atoi()?

Question:
create your own atoi()?


maybe an answer:


int atoi(char* str)
{
int ret=0, i=0, flag=0;
if(str == NULL)
return -1;

if(str[0] == '-')
{
flag = 1;
i++;
}

while(str[i] != '\0')
{
if(!(str[i]>=48 && str[i]<=57))
return -1;

ret = ret * 10 + ((int)str[i] - '0');
i++;
}

return !flag ? ret : ret*-1;
}

(Continued on next question...)

Other Interview Questions