Official Blog of G

What is the definition behind atoi()?

Posted in Global, Technology by G on January 12, 2009

Here is the C# code for the definition of atoi()

static int atoi(string str)
{
int sign=1;
int Number=0;
int tmp=0;
int x=0;
int l=(str.Length)-1;
char[] c=str.ToCharArray();
if ('-'==c[0])
{
sign=-1;
x=1;
}
for(;x ='0')&&(c[x]<='9'))
{

//0 is the 0 index
tmp=(c[x]-'0');

//simply multiply the number by 10 and add it
Number=Number*10 + tmp;
}
}
return Number*sign;
}

Just for fun!

Leave a Reply