-
Notifications
You must be signed in to change notification settings - Fork 7
/
myatoi.c
51 lines (42 loc) · 1022 Bytes
/
myatoi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <string.h>
int my_atoi(const char *str)
{
/* Check for a minus sign and skip past it. */
int negative = 0;
if (*str == '-') {
negative = 1;
str++;
}
/* Find the first non-numeric character. */
int index = 0;
while ('0' <= *str && *str <= '9') {
index++; str++;
}
/* If index is still 0, then there's no number. */
if (index == 0) return 0;
/* Now actually determine the value. */
int pow10 = 1;
int value = 0;
index--; str--;
while (index >= 0) {
int digit = *str - '0'; /*get value of digit*/
value += digit * pow10;
pow10 *= 10;
index--; str--;
}
/* Return the value, negated if necessary. */
if (negative) value *= -1;
return value;
}
int main(int argc, char *argv[])
{
printf("Please enter a number: ");
char buf[12]; /*enough room for "-2147483648\0"*/
fgets(buf, 12, stdin);
char *newline = strchr(buf, '\n');
if (newline) *newline = '\0';
printf("I got \"%s\".\n", buf);
printf("I interpreted that as %d.\n", my_atoi(buf));
return 0;
}