P176. Implement user defined ( atoi ) function.
#include<stdio.h>
#include<stdlib.h>
int
my_atoi(const char *);
void
main(int argc,char **argv)
{
int flag,i;
if(argc<2)
printf("Usage: a.out
String \n");
int num = my_atoi(argv[1]);
printf("%d\n",num);
}
int
my_atoi(const char *p)
{
int i,num;
if(p[0]=='-'|| p[0]=='+')
i=1;
else
i=0;
for(num=0;p[i];i++)
{
if(p[i]>'0' && p[i]<'9')
num=num*10+(p[i]-48);
else
break;
}
if(p[0]=='-')
num=-num;
return num;
}
Comments
Post a Comment