Skip to content

Instantly share code, notes, and snippets.

@MatthaeusHarris
Created September 19, 2017 21:11
Show Gist options
  • Save MatthaeusHarris/640e57de7eab60ebd8ac54e0352923b8 to your computer and use it in GitHub Desktop.
Save MatthaeusHarris/640e57de7eab60ebd8ac54e0352923b8 to your computer and use it in GitHub Desktop.
str_to_int function, with no bounds error checking
#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