int question15(char *string)
{
bool minus_punctuation = false;
int sum = 0;
if (*(string + 0) == ‘-‘) { minus_punctuation = true; string++; }
while (*string)
{
if (isdigit(*string)) { sum = sum * 10 + *string - ‘0‘; }
string++;
}
return minus_punctuation ? -sum : sum;
}
sum = sum * 10 + *string - ‘0‘;
for instance : "1234"
1 : sum = 0 * 10 + ‘1‘ - ‘0‘;
= 0 + 1 = 1;
2 : sum = 1 * 10 + ‘2‘ - ‘0‘;
= 10 + 2 = 12;
3 : sum = 12 * 10 + ‘3‘ - ‘0‘;
= 120 + 3 = 123;
4 : sum = 123 * 10 + ‘4‘ - ‘0‘;
= 1230 + 4 = 1234;
Achieve atoi function in the C langage
原文:https://www.cnblogs.com/constanted/p/14809909.html