Monday, July 23, 2012

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

#include<stdio.h>

int myatoi (const char *string);

int main()
{
    printf("\n%d\n", myatoi("1998"));
    return 0;
}

int myatoi(const char* string)
{
    int value = 0;

    if (string)
    {
        while (*string && (*string <= '9' && *string >= '0'))
        {
            value = (value * 10) + (*string - '0'); string++;
        }
    }
    return value;
}

No comments:

Post a Comment