[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Strings

Home > Programs > Strings

26 / 60

Write an efficient program to implemnt the atoi function.

Answer:

int myAtoi(char *str)
{
    int res = 0; /* Initialize result*/  
    /* Iterate through all characters of input string and update result*/
    for (int i = 0; str[i] != '\0'; ++i)
        res = res*10 + str[i] - '0';
  
    /* return result.*/
    return res;
}  
/* Driver program to test above function*/
int main()
{
    char str[] = "89789";
    int val = myAtoi(str);
  cout

Asked In ::

Post Your Answer Here:

Language:

Post Your Reply Here:



Language:

Post Your Reply Here:



Language:

Post Your Reply Here: