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 b9f0773 commit 9be7269
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ft_strmapi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/18 19:43:31 by ivanderw #+# #+# */
/* Updated: 2023/03/18 20:14:49 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
PROTOTYPE
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
PARAMETERS
s: The string on which to iterate
f: The function to apply to each character.
RETURN VALUE
The string created from the successive applications of 'f'
Returns NULL if the allocation fails.
EXTERNAL FUNCTS
malloc
DESCRIPTION
Applies the function 'f' to each character of the string 's', and passing
its index as the first argument to create a new string (with malloc(3)
resulting from successive applications of 'f'.
*/
#include "libft.h"

char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *out;
size_t i;

if (!s)
return (NULL);
out = (char *)malloc(sizeof(char) * ft_strlen(s) + 1);
if (!out)
return (NULL);
i = 0;
while (s[i] != '\0')
{
out[i] = f(i, s[i]);
i++;
}
out[i] = '\0';
return (out);
}
71 changes: 71 additions & 0 deletions ft_strncmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strncmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 17:37:49 by ivanderw #+# #+# */
/* Updated: 2023/03/14 17:43:27 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
SYNOPSIS
#include <string.h>
int
strcmp(const char *s1, const char *s2);
int
strncmp(const char *s1, const char *s2, size_t n);
DESCRIPTION
The strcmp() and strncmp() functions lexicographically compare the
null-termi-
nated strings s1 and s2.
The strncmp() function compares not more than n characters.
Because strncmp()
is designed for comparing strings rather than binary data,
characters that
appear after a `\0' character are not compared.
RETURN VALUES
The strcmp() and strncmp() functions return an integer greater than,
equal to,
or less than 0, according as the string s1 is greater than, equal to,
or less
than the string s2. The comparison is done using unsigned characters,
so that
`\200' is greater than `\0'.
*/
#include "libft.h"

int ft_strncmp(const char *s1, const char *s2, size_t n)
{
size_t i;
unsigned char *my_s1;
unsigned char *my_s2;

my_s1 = (unsigned char *)s1;
my_s2 = (unsigned char *)s2;
i = 0;
while (i < n)
{
if (my_s1[i] == '\0' && my_s2[i] == '\0')
{
return (0);
}
if (my_s1[i] != my_s2[i])
{
if (my_s1[i] == '\0' && my_s2[i] != '\0')
return (-1);
else if (my_s2[i] == '\0' && my_s1[i] != '\0')
return (1);
else
return (my_s1[i] - my_s2[i]);
}
i++;
}
return (0);
}
97 changes: 97 additions & 0 deletions ft_strnstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strnstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/14 18:15:45 by ivanderw #+# #+# */
/* Updated: 2023/03/21 19:04:58 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
NAME
strstr, strcasestr, strnstr -- locate a substring in a string
SYNOPSIS
#include <string.h>
char *
strnstr(const char *haystack, const char *needle, size_t len);
DESCRIPTION
The strstr() function locates the first occurrence of the null-terminated
string needle in the null-terminated string haystack.
The strnstr() function locates the first occurrence of the null-terminated
string needle in the string haystack, where not more than len characters
are searched. Characters that appear after a `\0' character are not
searched. Since the strnstr() function is a FreeBSD specific API, it
should only be used when portability is not a concern.
RETURN VALUES
If needle is an empty string, haystack is returned; if needle occurs
nowhere in haystack, NULL is returned; otherwise a pointer to the first
character of the first occurrence of needle is returned.
EXAMPLES
The following sets the pointer ptr to the "Bar Baz" portion of largestring:
const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strstr(largestring, smallstring);
The following sets the pointer ptr to NULL, because only the first 4
characters
of largestring are searched:
const char *largestring = "Foo Bar Baz";
const char *smallstring = "Bar";
char *ptr;
ptr = strnstr(largestring, smallstring, 4);
*/
#include <string.h>

static size_t ft_strlen(const char *s)
{
size_t i;

i = 0;
while (s[i] != '\0')
{
i++;
}
return (i);
}

char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
int temp_counter;

if (ft_strlen(needle) == 0)
return ((char *)haystack);
i = 0;
while (haystack[i] != '\0' && i < len)
{
if (haystack[i] == needle[0])
{
temp_counter = 0;
while (temp_counter < (int)ft_strlen(needle))
{
if ((i + temp_counter) >= len)
return ((void *) 0);
if (haystack[(i + temp_counter)] != needle[temp_counter])
break ;
temp_counter++;
if (temp_counter == (int)ft_strlen(needle))
return ((char *)&haystack[i]);
}
}
i++;
}
return ((void *) 0);
}
65 changes: 65 additions & 0 deletions ft_strrchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/06 16:47:45 by ivanderw #+# #+# */
/* Updated: 2023/03/14 17:48:57 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.
*/
#include <string.h>

static size_t ft_strlen(const char *s)
{
size_t i;

i = 0;
while (s[i] != '\0')
{
i++;
}
return (i);
}

char *ft_strrchr(const char *s, int c)
{
const char *o;

o = s + ft_strlen(s);
if (c == '\0')
return ((char *)s + ft_strlen(s));
while (o >= s)
{
if ((unsigned char)*o == (unsigned char)c)
{
return ((char *)o);
}
o--;
}
return (NULL);
}
54 changes: 54 additions & 0 deletions ft_strtrim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ivanderw <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/16 12:15:38 by ivanderw #+# #+# */
/* Updated: 2023/03/18 17:50:18 by ivanderw ### ########.fr */
/* */
/* ************************************************************************** */
/*
NAME
ft_strtrim
PROTOTYPE
char *ft_strtrim(char const *s1, char const *set);
PARAMETERS
s1: The string to be trimmed
set: The reference set of characters to trim
RETURN VALUE
The trimmed string.
NULL if the allocation fails.
EXTERNAL FUNCTS
malloc
DESCRIPTION
Allocates (with malloc(3)) and returns a copy of 's1' with the characters
specified ins 'set' removed from the beginning and the end of the string.
*/
#include "libft.h"

char *ft_strtrim(char const *s1, char const *set)
{
char *trim;
size_t start;
size_t end;

if (!s1 || !set)
return (NULL);
start = 0;
while (s1[start] && ft_strchr(set, s1[start]))
start++;
end = ft_strlen(s1);
while (end > start && ft_strchr(set, s1[end - 1]))
end--;
trim = ft_substr(s1, start, end - start);
if (!trim)
return (NULL);
return (trim);
}

0 comments on commit 9be7269

Please sign in to comment.