Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First 6 exercises #85

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tasks from the first days (Nevy)
  • Loading branch information
Nevyana Angelova authored and Nevyana Angelova committed Dec 18, 2021
commit f869a790f480d3f119fc93a6e6faaff2052de607
25 changes: 25 additions & 0 deletions courses/cunix2/libft/libft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <unistd.h>
#include <stdlib.h>

void ft_bzero(void *s, size_t n);
char *ft_strchr(const char *s, int chr);
int ft_isalpha(int chr);
int ft_isdigit(int chr);
int ft_isascii(int chr);
int ft_tolower(int chr);
int ft_toupper(int chr);

int ft_abs(int i);
div_t ft_div(int numer, int denom);
char *ft_strstr(const char *s1, const char *s2);
char *ft_strnstr(const char *haystack, const char *needle, size_t len);
void *ft_memset(void *s, int c, size_t n);
void *ft_memcpy(void *dest, const void *src, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);

void ft_striter(char *str, void (*fn)(char *));
char *ft_strjoin(char const *s1, char const *s2);
char *ft_strtrim(char const *s);
char **ft_strsplit(char const *s, char c);

int unsigned my_strlen(char const *str);
6 changes: 6 additions & 0 deletions courses/cunix2/libft/src/ft_abs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../libft.h"

int ft_abs(int i)
{
return (i < 0) ? -i : i;
}
12 changes: 12 additions & 0 deletions courses/cunix2/libft/src/ft_bzero.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdlib.h>
#include "../libft.h"

void ft_bzero(void *s, size_t n)
{
char *str = s;
while (n > 0)
{
*str++ = '\0';
n--;
}
}
14 changes: 14 additions & 0 deletions courses/cunix2/libft/src/ft_div.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../libft.h"

div_t ft_div(int numer, int denom)
{
div_t tmp;
if (!denom)
{
tmp.quot = tmp.rem = 0;
return tmp;
}
tmp.quot = numer / denom;
tmp.rem = numer % denom;
return tmp;
}
7 changes: 7 additions & 0 deletions courses/cunix2/libft/src/ft_isalpha.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "../libft.h"

int ft_isalpha(int chr)
{
chr = (char)chr;
return (('A' <= chr && chr <= 'Z') || ('a' <= chr && chr <= 'z'));
}
6 changes: 6 additions & 0 deletions courses/cunix2/libft/src/ft_isascii.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../libft.h"

int ft_isascii(int chr)
{
return (chr >= 0) ? chr == (char)chr : 0;
}
6 changes: 6 additions & 0 deletions courses/cunix2/libft/src/ft_isdigit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "../libft.h"

int ft_isdigit(int dig)
{
return ((dig >= '0') && (dig <= '9'));
}
14 changes: 14 additions & 0 deletions courses/cunix2/libft/src/ft_memcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../libft.h"

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *tmp_s1 = s1, *tmp_s2 = s2;
for (size_t i = 0; i < n; i++)
{
if (tmp_s1[i] != tmp_s2[i])
{
return tmp_s1[i] - tmp_s2[i];
}
}
return 0;
}
12 changes: 12 additions & 0 deletions courses/cunix2/libft/src/ft_memcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "../libft.h"

void *ft_memcpy(void *dest, const void *src, size_t n)
{
char *tmp_dest = dest;
const char *tmp_src = src;
for (size_t i = 0; i < n; i++)
{
*tmp_dest++ = *tmp_src++;
}
return dest;
}
11 changes: 11 additions & 0 deletions courses/cunix2/libft/src/ft_memset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "../libft.h"

void *ft_memset(void *s, int c, size_t n)
{
char *tmp = s;
for (size_t i = 0; i < n; i++)
{
tmp[i] = c;
}
return s;
}
14 changes: 14 additions & 0 deletions courses/cunix2/libft/src/ft_strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "../libft.h"

char *ft_strchr(const char *s, int c)
{
c = (char)c;
do
{
if (*s == c)
{
return (char *)s;
}
} while (*s++);
return NULL;
}
9 changes: 9 additions & 0 deletions courses/cunix2/libft/src/ft_striter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "../libft.h"

void ft_striter(char *str, void (*fn)(char *))
{
while (*str)
{
(*fn)(str++);
}
}
21 changes: 21 additions & 0 deletions courses/cunix2/libft/src/ft_strjoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "../libft.h"

char *ft_strjoin(const char *s1, const char *s2)
{
char *tmp;
if (!(tmp = (char *)malloc(my_strlen((char *)s1) + my_strlen((char *)s2) + 1)))
{
return NULL;
}
size_t i = 0;
for (int j = 0; s1[j]; j++)
{
tmp[i++] = s1[j];
}
for (int j = 0; s2[j]; j++)
{
tmp[i++] = s2[j];
}
tmp[i] = '\0';
return tmp;
}
31 changes: 31 additions & 0 deletions courses/cunix2/libft/src/ft_strnstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../libft.h"

char *ft_strnstr(const char *haystack, const char *needle, size_t len)
{
size_t i;
size_t c;
size_t n_len;
char *hay;

i = 0;
hay = (char *)haystack;
n_len = my_strlen(needle);
if (n_len == 0 || haystack == needle)
{
return (hay);
}
while (hay[i] != '\0' && i < len)
{
c = 0;
while (hay[i + c] != '\0' && needle[c] != '\0' && hay[i + c] == needle[c] && i + c < len)
{
c++;
}
if (c == n_len)
{
return (hay + i);
}
i++;
}
return (0);
}
91 changes: 91 additions & 0 deletions courses/cunix2/libft/src/ft_strsplit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include "../libft.h"

static size_t ft_count_words(const char *s, char c)
{
size_t words;
size_t i;

words = 0;
i = 0;
while (s[i] != '\0')
{
if ((i == 0 || s[i - 1] == c) && s[i] != c)
{
words++;
}
i++;
}
return (words);
}

static size_t ft_wordlen(char const *s, char c)
{
size_t i;

i = 0;
while (*s)
{
if (*s != c)
{
s++;
i++;
}
else
{
return (i);
}
}
return (i);
}

char *ft_strncpy(char *dst, char const *src, size_t n)
{
size_t i;

i = -1;
if (dst == NULL || src == NULL)
{
return (NULL);
}
while (++i < n && src[i])
{
dst[i] = src[i];
}
while (i < n--)
{
dst[i] = '\0';
}
return (dst);
}

char **ft_strsplit(char const *s, char c)
{
char **word_tab;
size_t nb_words;
size_t word_len;
size_t i;
size_t n;

i = 0;
n = 0;
nb_words = ft_count_words(s, c);
word_tab = (char **)malloc(sizeof(char *) * (nb_words + 1));
while (n < nb_words && word_tab)
{
while (s[i] != '\0' && s[i] == c)
{
i++;
}
word_len = ft_wordlen(&s[i], c);
word_tab[n] = malloc(sizeof(**word_tab) * (word_len + 1));
ft_strncpy(word_tab[n], &s[i], word_len);
word_tab[n][word_len] = '\0';
while (s[i] != '\0' && s[i] != c)
{
i++;
}
n++;
}
word_tab[n] = NULL;
return (word_tab);
}
27 changes: 27 additions & 0 deletions courses/cunix2/libft/src/ft_strtrim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "../libft.h"

char *ft_strtrim(const char *str)
{
while (*str && (*str == ' ' || *str == ',' || *str == '\n' || *str == '\t'))
{
str++;
}
const char *tmp = str + my_strlen((char *)str);
while (tmp > str && (*(tmp - 1) == ' ' || *(tmp - 1) == ',' || *(tmp - 1) == '\n' || *(tmp - 1) == '\t'))
{
tmp--;
}

char *cutted;
if (!(cutted = (char *)malloc(tmp - str + 1)))
{
return NULL;
}
size_t i;
for (i = 0; str + i < tmp; i++)
{
cutted[i] = str[i];
}
cutted[i] = '\0';
return cutted;
}
7 changes: 7 additions & 0 deletions courses/cunix2/libft/src/ft_tolower.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "../libft.h"

int ft_tolower(int chr)
{
chr = (char)chr;
return ('A' <= chr && chr <= 'Z') ? chr + ('a' - 'A') : chr;
}
16 changes: 16 additions & 0 deletions courses/cunix2/libft/src/ft_toupper.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "../libft.h"

int ft_toupper(int chr)
{

chr = (char)chr;

if ('a' <= chr && chr <= 'z')
{
return chr - ('a' - 'A');
}
else
{
return chr;
}
}
10 changes: 10 additions & 0 deletions courses/cunix2/libft/utils/my_strlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <string.h>

int unsigned my_strlen(char *str)
{
int i = 0;
while (str[i] != '\0')
++i;
return i;
}