Get Off-Campus Placement Jobs Info !!!
Programs asked in Mettl Coding Round
Click To Practce List of Programs asked in Nagarro !!!

Program Discussion :: Strings

Home > Programs > Strings

6 / 59

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: