Skip to content

Instantly share code, notes, and snippets.

@TheEmpty
Created January 28, 2015 17:02
Show Gist options
  • Save TheEmpty/43d3e95655b42e71a72e to your computer and use it in GitHub Desktop.
Save TheEmpty/43d3e95655b42e71a72e to your computer and use it in GitHub Desktop.
#include <stdio.h> // printf
#include <string.h> // strlen
#include <math.h> // pow
int intFromAscii(char ascii) {
switch(ascii) {
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: return 0;
}
}
int main(int argc, const char** args) {
if(argc <= 1) return 0;
const char* input = args[1];
int total = 0;
int len = strlen(input);
for(int i = len-1; i+1 > 0; i--) {
int power = len - i - 1;
total += pow(10, power) * intFromAscii(input[i]);
}
if(input[0] == '-') total = total * -1;
return total;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment