first commit!
This commit is contained in:
27
atoi_v2.c
Normal file
27
atoi_v2.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int atoi(char string[]) {
|
||||
int i, sign_ext, res;
|
||||
|
||||
for (i = 0; isspace(string[i]); ++i)
|
||||
;
|
||||
|
||||
sign_ext = string[i] == '-' ? -1 : 1;
|
||||
if (string[i] == '-')
|
||||
++i;
|
||||
|
||||
for (res = 0; isdigit(string[i]); ++i) {
|
||||
res = res * 10 + (string[i] - '0');
|
||||
}
|
||||
|
||||
return sign_ext * res;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("%d\n", atoi("123"));
|
||||
printf("%d\n", atoi("-123"));
|
||||
printf("%d\n", atoi("+123"));
|
||||
printf("%d\n", atoi("0123"));
|
||||
printf("%d\n", atoi("0123.1"));
|
||||
}
|
Reference in New Issue
Block a user