How to implement your own atoi() function?

This program demonstrates how to implement your own atoi() function.

It uses iterative method (simplest way and easy to understand) for doing so.

You can compile this program on bOtskOOl Free Online C/C++ Compiler


//How to implement your own atoi() function
/*
**********************
www.botskool.com
**********************
*/

#include<stdio.h>

int myatoi(const char *string);

int main(int argc, char* argv[])

{

printf("\n%d\n", myatoi("2010"));

return(0);

}

int myatoi(const char *string)

{

int i;

i=0;

while(*string)

{

i=(i<<3) + (i<<1) + (*string - '0');

string++;

// Dont increment i!

}

return(i);

}


The Output is shown below.
 

How to implement atoi() function

The above OUTPUT was generated by bOtskOOl Online Compiler - Try it out now>>

Download Source Code

Tags: