int ft_atoi(const char *str)
#include "libft.h"
static int is_space(char c)
{
if (c == '\n' || c == '\t' || c == '\r' || c == '\f' || c == '\v'
|| c == ' ')
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int i;
int neg;
i = 0;
neg = 0;
while (is_space(*str))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
neg++;
str++;
}
while (*str >= '0' && *str <= '9')
{
i = i * 10 + (*str - '0');
str++;
}
if (neg % 2 != 0)
i = i * -1;
return (i);
}
is_space로 문자열의 시작공백을 없앤 후 변환
#include <stdio.h>
int main(void)
{
printf("%d\n", ft_atoi(" -42")); // -42
printf("%d\n", ft_atoi("4193abc")); // 4193
printf("%d\n", ft_atoi("+1234")); // 1234
printf("%d\n", ft_atoi(" \t\v\n42")); // 42
return (0);
}