Last active
August 29, 2015 14:15
-
-
Save alfhh/20d627db7867c6890d88 to your computer and use it in GitHub Desktop.
Implementation of atoi function in C.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int atoi(char *s) { | |
int acum = 0; | |
while((*s >= '0')&&(*s <= '9')) { | |
acum = acum * 10; | |
acum = acum + (*s - 48); | |
s++; | |
} | |
return (acum); | |
} | |
//UPGRADE FOR NEGATIVE NUMBERS | |
int atoi(char *s) { | |
int acum = 0; | |
int factor = 1; | |
if(*s == '-') { | |
factor = -1; | |
s++; | |
} | |
while((*s >= '0')&&(*s <= '9')) { | |
acum = acum * 10; | |
acum = acum + (*s - 48); | |
s++; | |
} | |
return (factor * acum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment