Advertisement
EWTD

Untitled

Nov 4th, 2020
2,265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.86 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int len(long nb){
  5.     int len;
  6.     len = 0;
  7.     if (nb < 0){
  8.         nb = nb * -1;
  9.         len++;
  10.     }
  11.     if (nb == 0)
  12.         return (1);
  13.     while (nb > 0){
  14.         nb = nb / 10;
  15.         len++;
  16.     }
  17.     return (len);
  18. }
  19. char* ft_itoa(int nb){
  20.     char *str;
  21.     int i;
  22.  
  23.     if (nb <= -2147483648){
  24.         return "-2147483648";
  25.     }
  26.     i = len(nb);
  27.     if (!(str = (char *)malloc(sizeof(char) * (i + 1))))
  28.         return (NULL);
  29.     str[i--] = '\0';
  30.     if (nb == 0){
  31.         str[0] = 48;
  32.         return (str);
  33.     }
  34.     if (nb < 0){
  35.         str[0] = '-';
  36.         nb = nb * -1;
  37.     }
  38.     while (nb > 0){
  39.         str[i] = '0' + (nb % 10);
  40.         nb = nb / 10;
  41.         i--;
  42.     }
  43.     return (str);
  44. }
  45. int main(){
  46.     int n = 2147483648;
  47.     printf("%s\n", ft_itoa(n));
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement