Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
vanderhammer91 authored Mar 22, 2023
1 parent f42360a commit bb24fb1
Show file tree
Hide file tree
Showing 5 changed files with 298 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ft_putnbr_fd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 17:10:16 by ivanderw #+# #+# */
/* Updated: 2023/03/18 17:20:17 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
PROTOTYPE
void ft_putnbr_fd(int n, int fd);
PARAMETERS
n: The integer to output.
fd: The file descriptor on which to write.
EXTERNAL FUNCTIONS
write
DESCRIPTION
Outputs the integer 'n' to the given file descriptor.
*/
#include "libft.h"

void ft_putnbr_fd(int n, int fd)
{
if (n == -2147483648)
{
ft_putstr_fd("-2147483648", fd);
}
else if (n < 0)
{
ft_putchar_fd('-', fd);
ft_putnbr_fd (-n, fd);
}
else if (n >= 10)
{
ft_putnbr_fd(n / 10, fd);
ft_putchar_fd(n % 10 + '0', fd);
}
else
{
ft_putchar_fd(n + '0', fd);
}
}
38 changes: 38 additions & 0 deletions ft_putstr_fd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 16:40:55 by ivanderw #+# #+# */
/* Updated: 2023/03/18 16:56:21 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
PROTOTYPE
void ft_putstr_fd(char *s, int fd);
PARAMETERS
s: The string to output
fd: the file descriptor on which to write.
EXTERNAL FUNCTIONS
write
DESCRIPTION
Outputs the string 's' to the given file descriptor.
*/
#include "libft.h"

void ft_putstr_fd(char *s, int fd)
{
int i;

i = 0;
while (s[i] != '\0')
{
ft_putchar_fd(s[i], fd);
i++;
}
}
104 changes: 104 additions & 0 deletions ft_split.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/16 17:01:54 by ivanderw #+# #+# */
/* Updated: 2023/03/21 12:33:27 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
NAME
ft_split
PROTOTYPE
char **ft_split(char const *s, char c);
PARAMETERS
s: The string to be split.
c: The delimiter character.
RETURN
The array of new strings resulting from the split
NULL if the allocation fails
EXTERNAL FUNCTIONS
malloc, free
DESCRIPTION
Allocates (with malloc(3)) and returns an array of strings obtained by
splitting 's' using the character 'c' as a delimiter. The array must end
with a NULL pointer.
*/
#include "libft.h"

static size_t get_word_count(char const *s, char c)
{
size_t word_count;
size_t is_word;

is_word = 0;
word_count = 0;
while (*s)
{
if (*s != c && is_word == 0)
{
is_word = 1;
word_count++;
}
else if (*s == c)
is_word = 0;
s++;
}
return (word_count);
}

static char *ft_strndup(const char *s, size_t n)
{
char *out;
size_t i;

out = (char *)malloc(n + 1);
if (!out)
return (NULL);
i = 0;
while (i < n && s[i])
{
out[i] = s[i];
i++;
}
while (i <= n)
out[i++] = 0;
return (out);
}

char **ft_split(char const *s, char c)
{
char **split;
size_t word_count;
size_t i;
size_t j;

if (!s)
return (0);
word_count = get_word_count(s, c);
split = malloc(sizeof(char *) * (word_count + 1));
if (!split)
return (0);
i = 0;
j = 0;
while (i < word_count)
{
while (s[j] == c)
j++;
s = &s[j];
j = 0;
while (s[j] != c && s[j])
j++;
split[i++] = ft_strndup(s, j);
}
split[i] = 0;
return (split);
}
53 changes: 53 additions & 0 deletions ft_strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 15:49:48 by ivanderw #+# #+# */
/* Updated: 2023/03/16 16:22:28 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
SYNOPSIS
#include <string.h>
char *
strchr(const char *s, int c);
char *
strrchr(const char *s, int c);
DESCRIPTION
The strchr() function locates the first occurrence of c (converted to a
char) in the string pointed to by s. The terminating null character is
considered to be part of the string; therefore if c is `\0', the functions
locate the terminating `\0'.
The strrchr() function is identical to strchr(), except it locates the
last occurrence of c.
RETURN VALUES
The functions strchr() and strrchr() return a pointer to the located char-
acter, or NULL if the character does not appear in the string.
NOTES TO FUTURE SELF:
*/
#include "libft.h"

char *ft_strchr(const char *s, int c)
{
unsigned char *ptr;

ptr = (unsigned char *)s;
while (*ptr)
{
if (*ptr == (unsigned char)c)
return ((char *)ptr);
ptr++;
}
if (*ptr == (unsigned char)c)
return ((char *)ptr);
return (NULL);
}
55 changes: 55 additions & 0 deletions ft_strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/15 14:37:57 by ivanderw #+# #+# */
/* Updated: 2023/03/15 17:00:09 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
NAME
strdup, strndup -- save a copy of a string
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
char *
strdup(const char *s1);
char *
strndup(const char *s1, size_t n);
DESCRIPTION
The strdup() function allocates sufficient memory for a copy of the
string s1, does the copy, and returns a pointer to it.
The pointer may subsequently be used as an argument to the function
free(3).
If insufficient memory is available, NULL is returned and errno is set
to ENOMEM.
*/
#include "libft.h"

char *ft_strdup(char *src)
{
char *output;
int i;

output = (char *) malloc(ft_strlen(src) + 1);
if (!(output))
return (NULL);
i = 0;
while (src[i] != '\0')
{
output[i] = src[i];
i++;
}
output[i] = '\0';
return (output);
}

0 comments on commit bb24fb1

Please sign in to comment.