Q21 Write your own program to implement the atoi() function.

What atoi() function does?
atoi() function is used to convert a string to an integer

#include
int own_atoi(const char *str)
{

int value = 0;
if(str)
{

while(*str && (*str != ‘0’))

{

value = (value * 10) + (*str – ‘0’);
str++;

}

}
return value;

}
int main()
{

int value;
value = own_atoi(“2345678”);
printf(“value = %d\n”, value);
return 0;

}

Output:

$ ./a.exe
value = 2345678

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.