Skip to content

Instantly share code, notes, and snippets.

@alfhh
Last active August 29, 2015 14:15
Show Gist options
  • Save alfhh/20d627db7867c6890d88 to your computer and use it in GitHub Desktop.
Save alfhh/20d627db7867c6890d88 to your computer and use it in GitHub Desktop.
Implementation of atoi function in C.
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