-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyAtoi.c
60 lines (54 loc) · 919 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
52
53
54
55
56
57
58
59
60
# include <stdio.h>
# include <stdlib.h>
int myAtoi(char* str) {
int sign = 1;
long long sum = 0;
char *pstr = str, flag = 0, overflow = 0;
for (pstr; *pstr != '\0'; pstr++) {
if (*pstr == ' ') {
if (!flag)
continue;
else
break;
}
if ((!flag) && *pstr == '-') {
if (*(pstr + 1) <= '9' && *(pstr + 1) >= '0') {
flag = 1;
sign = -1;
continue;
}
else {
break;
}
}
else if ((!flag) && *pstr == '+') {
if (*(pstr + 1) <= '9' && *(pstr + 1) >= '0') {
flag = 1;
sign = 1;
continue;
}
else {
break;
}
}
else if (*pstr > '9' || *pstr < '0')
break;
if (flag == 0)
flag = 1;
sum *= 10;
sum += sign * (*pstr-'0');
if (sum > INT_MAX)
return INT_MAX;
else if (sum < INT_MIN)
return INT_MIN;
}
if (flag == 0)
return 0;
else
return (int)sum;
}
int main()
{
printf("%d\n", myAtoi(" -0012a42"));
return 0;
}