hello I'm trying to recreate atoi in with some other added conditions, mainly computing how many signs and returning the number positive if number of minus even , otherwise negative, the script works fine. the problem it should be created with int type and it will be tested with some really big numbers, such as 19999999999
, I tried to cast it's return value but stupidly forget it will return always it's type. any idea ? thanks
#include <stdio.h>
int ft_checksign(char *str)
{
int i;
int sign;
sign = 0;
i = 0;
while (*(str + i))
{
if (*(str + i) == '-')
sign += 1;
i++;
}
return (sign);
}
int ft_atoi(char *str)
{
long long result;
int i;
i = 0;
result = 0;
while ((*(str + i) >= 9 && *(str + i) <= 32)
|| *(str + i) == '-' || *(str + i) == '+')
i++;
while (*(str + i) && (*(str + i) >= '0' && *(str + i) <= '9'))
{
result *= 10;
result += (long long)str[i] - '0';
i++;
}
if (ft_checksign(str) % 2 == 1)
return ((long long)-result);
else
return ((long long)result);
}
int main()
{
printf("%i", ft_atoi("19999999999"));
return 0;
}
OUTPUT
-1474836481