Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- int len(long nb){
- int len;
- len = 0;
- if (nb < 0){
- nb = nb * -1;
- len++;
- }
- if (nb == 0)
- return (1);
- while (nb > 0){
- nb = nb / 10;
- len++;
- }
- return (len);
- }
- char* ft_itoa(int nb){
- char *str;
- int i;
- if (nb <= -2147483648){
- return "-2147483648";
- }
- i = len(nb);
- if (!(str = (char *)malloc(sizeof(char) * (i + 1))))
- return (NULL);
- str[i--] = '\0';
- if (nb == 0){
- str[0] = 48;
- return (str);
- }
- if (nb < 0){
- str[0] = '-';
- nb = nb * -1;
- }
- while (nb > 0){
- str[i] = '0' + (nb % 10);
- nb = nb / 10;
- i--;
- }
- return (str);
- }
- int main(){
- int n = 2147483648;
- printf("%s\n", ft_itoa(n));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement