Created
September 19, 2017 21:11
-
-
Save MatthaeusHarris/640e57de7eab60ebd8ac54e0352923b8 to your computer and use it in GitHub Desktop.
str_to_int function, with no bounds error checking
This file contains 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
#include <stdio.h> | |
int char_is_int(char c) { | |
return c >= '0' && c <= '9'; | |
} | |
int char_to_int(char c) { | |
return c - '0'; | |
} | |
int str_to_int(char *s) { | |
int acc = 0; | |
while(*s) { | |
if (char_is_int(*s)) { | |
acc *= 10; | |
acc += char_to_int(*s); | |
} | |
++s; | |
} | |
return acc; | |
} | |
int main(int argc, char **argv) { | |
printf("%d\n", str_to_int(argv[1])); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment