70 lines
1.6 KiB
C
70 lines
1.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_atoi.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/06/11 15:22:52 by whaffman #+# #+# */
|
|
/* Updated: 2024/06/12 20:21:53 by whaffman ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
int ft_isdigit(char c)
|
|
{
|
|
return (c >= '0' && c <= '9');
|
|
}
|
|
|
|
int ft_isspace(char c)
|
|
{
|
|
return (' ' == c || '\f' == c || \
|
|
'\n' == c || '\r' == c || \
|
|
'\t' == c || '\v' == c);
|
|
}
|
|
|
|
int ft_atoi(char *str)
|
|
{
|
|
int sign;
|
|
int res;
|
|
|
|
while (ft_isspace(*str))
|
|
str++;
|
|
res = 0;
|
|
sign = 1;
|
|
while (*str == '-' || *str == '+')
|
|
{
|
|
if (*str == '-')
|
|
sign *= -1;
|
|
str++;
|
|
}
|
|
while (ft_isdigit(*str))
|
|
res = 10 * res + sign * (*str++ - '0');
|
|
return (res);
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
void test(char *str)
|
|
{
|
|
printf("string: \"%s\"\n", str);
|
|
printf("int: %d\n", ft_atoi(str));
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
test("");
|
|
test("123");
|
|
test("-123");
|
|
test("-2147483648");
|
|
test("2147483647");
|
|
test(" ----+++-1234");
|
|
test("12345a1234");
|
|
test("123.34");
|
|
return (0);
|
|
}
|
|
|
|
#endif
|