ft_utoa.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* ************************************************************************** */
  2. /* */
  3. /* ::: :::::::: */
  4. /* ft_utoa.c :+: :+: :+: */
  5. /* +:+ +:+ +:+ */
  6. /* By: bchanot <bchanot@students.42.fr> +#+ +:+ +#+ */
  7. /* +#+#+#+#+#+ +#+ */
  8. /* Created: 2016/02/11 19:54:05 by bchanot #+# #+# */
  9. /* Updated: 2016/05/15 21:31:00 by bchanot ### ########.fr */
  10. /* */
  11. /* ************************************************************************** */
  12. #include "libft.h"
  13. #include <stdlib.h>
  14. char *ft_utoa(long long unsigned int nb)
  15. {
  16. long long unsigned int x;
  17. char *str;
  18. int cpt;
  19. x = nb;
  20. cpt = 0;
  21. while (x > 10)
  22. {
  23. x /= 10;
  24. cpt++;
  25. }
  26. str = (char *)malloc(sizeof(char *) * cpt + 1);
  27. if (str)
  28. {
  29. str[cpt + 1] = '\0';
  30. while (cpt >= 0)
  31. {
  32. x = nb % 10;
  33. str[cpt] = 48 + x;
  34. nb /= 10;
  35. cpt--;
  36. }
  37. }
  38. return (str);
  39. }