Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
ft_itoa
Browse files Browse the repository at this point in the history
  • Loading branch information
nopbxlr committed May 9, 2022
1 parent 70cfe05 commit b97351f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
71 changes: 71 additions & 0 deletions ft_itoa.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ctherin <ctherin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/09 16:58:14 by ctherin #+# #+# */
/* Updated: 2022/05/09 17:22:48 by ctherin ### ########.fr */
/* */
/* ************************************************************************** */

#include"libft.h"

int get_nb_len(int n)
{
long nl;
int len;
int is_neg;

nl = n;
len = 0;
is_neg = 0;
if (nl == 0)
return (1);
if (nl < 0)
{
nl = -nl;
is_neg = 1;
}
while (nl > 0)
{
len++;
nl /= 10;
}
return (len + is_neg);
}

char *ft_itoa(int n)
{
char *ptr;
int nb_len;
long nl;

nb_len = get_nb_len(n);
nl = n;
ptr = ft_calloc(nb_len, sizeof(char));
if (!ptr)
return (NULL);
if (nl < 0)
{
nl = -nl;
ptr[0] = '-';
}
if (nl == 0)
ptr[0] = '0';
while (nl > 0)
{
ptr[nb_len - 1] = (nl % 10) + '0';
nl /= 10;
nb_len--;
}
return (ptr);
}

/*#include<stdio.h>
int main()
{
printf("%s", ft_itoa(12));
return (0);
}*/
3 changes: 2 additions & 1 deletion libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ctherin <ctherin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 17:49:13 by ctherin #+# #+# */
/* Updated: 2022/05/09 15:54:22 by ctherin ### ########.fr */
/* Updated: 2022/05/09 17:21:37 by ctherin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,5 +43,6 @@ void *ft_strrchr(const void *s, int c);
int ft_atoi(const char *nptr);
char *ft_strjoin(char const *s1, char const *s2);
char **ft_split(const char *s, char c);
char *ft_itoa(int n);

#endif

0 comments on commit b97351f

Please sign in to comment.