Files
first-repo/atoi_v2.c
2025-02-06 12:54:34 +02:00

28 lines
502 B
C

#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"));
}