Pages

Thursday, 17 September 2015

C program to implement the atoi() function

User defined atoi() function

Coding:
#include<stdio.h>
long myatoi(const char *s)
{
 long unsigned int val= 0;
 while (*s && (*s <= '9' && *s>= '0'))
 {
   val = (val* 10) + (*s - '0');
   s++;
 }
 return val;
}
int main()
{
 printf("Converted string into number:\n%lu\n", myatoi("1456"));
 return(0);
}

Output: 
Converted string into number:
1456