Skip to content

Commit

Permalink
add: atoi func
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed May 1, 2024
1 parent 2847ae7 commit e17889f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ VPATH = src:src/ascii:src/memory:src/string
BUILD_DIR = build

SRCS = ft_isalnum.c ft_isalpha.c ft_isascii.c ft_isdigit.c ft_isprint.c \
ft_toupper.c ft_tolower.c \
ft_toupper.c ft_tolower.c ft_atoi.c \
ft_strlen.c ft_strlcpy.c ft_strlcat.c ft_strchr.c ft_strrchr.c \
ft_strncmp.c ft_strnstr.c \
ft_memset.c ft_memcpy.c ft_memchr.c ft_memmove.c ft_bzero.c \
Expand Down
3 changes: 2 additions & 1 deletion include/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ebabaogl <ebabaogl@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 16:05:11 by ebabaogl #+# #+# */
/* Updated: 2024/04/27 23:22:02 by ebabaogl ### ########.fr */
/* Updated: 2024/05/01 15:35:19 by ebabaogl ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,6 +22,7 @@ int ft_isdigit(int c);
int ft_isprint(int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_atoi(const char *nptr);

size_t ft_strlen(const char *s);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
Expand Down
37 changes: 37 additions & 0 deletions src/ascii/ft_atoi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebabaogl <ebabaogl@student.42kocaeli.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/01 14:48:47 by ebabaogl #+# #+# */
/* Updated: 2024/05/01 15:33:49 by ebabaogl ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>
#include <stdio.h>

int ft_atoi(const char *nptr)
{
int sign;
int total;

sign = 1;
total = 0;
while (*nptr == ' ' || (*nptr >= 9 && *nptr <= 13))
nptr++;
if (*nptr == '-' || *nptr == '+')
{
sign = 44 - *nptr;
nptr++;
}
while (*nptr >= '0' && *nptr <= '9')
{
total *= 10;
total += *nptr - '0';
nptr++;
}
return (sign * total);
}

0 comments on commit e17889f

Please sign in to comment.