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

Week3 #91

Closed
wants to merge 5 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
Next Next commit
week3
  • Loading branch information
singhpreet86 committed Feb 23, 2022
commit 1aaf2c6bacde4b9cafdd29229c48e56ce6bdb9f7
Binary file added courses/cunix2/libft/libft.a
Binary file not shown.
29 changes: 29 additions & 0 deletions courses/cunix2/libft/libft.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef LIBFT_H
#define LIBFT_H
#include <stddef.h>
#include <stdlib.h>

void ft_bzero(void *s, size_t n);
char *ft_strdup(const char *s);
char *ft_strchr(const char *s, int c);
int ft_isalpha(int c);
int ft_isdigit(int c);
int ft_isascii(int c);
int ft_toupper(int c);
int ft_tolower(int c);
int ft_abs(int j);
div_t ft_div(int num, int denom);
void *ft_memcpy(void *__restrict__ dest, const void *__restrict__ src, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n);
char *ft_strnstr(const char *haystack, const char *needle, size_t n);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strjoin(char const *s1, char const *s2);
size_t ft_strlen(const char *s);
char *ft_strtrim(char const *s);
char **ft_strsplit(char const *s, char c);
unsigned int ft_words(const char *s, char c);
size_t ft_strclen(const char *s, char c);
void ft_striter(char *s, void (*f)(char *));
void *ft_memset(void *s, int c, size_t n);
char *ft_strstr(const char *haystack, const char *needle);
#endif
4 changes: 4 additions & 0 deletions courses/cunix2/libft/src/ft_abs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int ft_abs(int j)
{
return (j < 0 ? -j : j);
}
11 changes: 11 additions & 0 deletions courses/cunix2/libft/src/ft_bzero.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stddef.h>

void ft_bzero(void *s, size_t n)
{
char *data = (char *) s;

while (n-- != 0)
{
*data++ = '\0';
}
}
12 changes: 12 additions & 0 deletions courses/cunix2/libft/src/ft_div.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdlib.h>

div_t ft_div(int numerator, int denominator)
{
div_t res =
{
numerator / denominator,
numerator % denominator
};

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

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

int ft_memcmp(const void *s1, const void *s2, size_t n)
{
unsigned char *s1_pos = (unsigned char *) s1;
unsigned char *s2_pos = (unsigned char *) s2;
int diff;

while (n-- != 0)
{
diff = *s1_pos++ - *s2_pos++;

if (diff != 0)
{
return diff;
}
}

return 0;
}
15 changes: 15 additions & 0 deletions courses/cunix2/libft/src/ft_memcpy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stddef.h>
#include <stdio.h>

void *ft_memcpy(void *restrict dest, const void *restrict src, size_t n)
{
unsigned char *dest_pos = (unsigned char *) dest;
unsigned char *src_pos = (unsigned char *) src;

while (n-- != 0)
{
*dest_pos++ = *src_pos++;
}

return dest;
}
13 changes: 13 additions & 0 deletions courses/cunix2/libft/src/ft_memset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stddef.h>

void *ft_memset(void *s, int c, size_t n)
{
unsigned char *pos = (unsigned char *) s;

while (n-- != 0)
{
*pos++ = (unsigned char) c;
}

return s;
}
18 changes: 18 additions & 0 deletions courses/cunix2/libft/src/ft_strchr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stddef.h>

char *ft_strchr(const char *s, int c)
{
c %= 256;

while (*s != '\0')
{
if (*s == c)
{
return (char *) s;
}

s++;
}

return (*s == c) ? (char *) s : 0;
}
13 changes: 13 additions & 0 deletions courses/cunix2/libft/src/ft_strclen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "../libft.h"

size_t ft_strclen(const char *s, char c)
{
char *word_end = ft_strchr(s, c);

if (!word_end)
{
return ft_strlen(s);
}

return word_end - s;
}
8 changes: 8 additions & 0 deletions courses/cunix2/libft/src/ft_striter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
void ft_striter(char *s, void (*f)(char *))
{
while (*s != '\0')
{
f(s);
s++;
}
}
30 changes: 30 additions & 0 deletions courses/cunix2/libft/src/ft_strjoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <stdlib.h>
#include "../libft.h"

char *ft_strjoin(const char *s1, const char *s2)
{
size_t l1 = ft_strlen(s1);
size_t l2 = ft_strlen(s2);
size_t concat_l = l1 + l2;

char *concat = (char *) malloc(sizeof(char) * (concat_l + 1));

if (!concat)
{
return 0;
}

while (l1-- != 0)
{
*concat++ = *s1++;
}

while (l2-- != 0)
{
*concat++ = *s2++;
}

*concat = '\0';

return concat - concat_l;
}
13 changes: 13 additions & 0 deletions courses/cunix2/libft/src/ft_strlen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stddef.h>

size_t ft_strlen(const char *s)
{
size_t len = 0;

while (*s++ != '\0')
{
len++;
}

return len;
}
21 changes: 21 additions & 0 deletions courses/cunix2/libft/src/ft_strncmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stddef.h>

int ft_strncmp(const char *s1, const char *s2, size_t n)
{
for (size_t i = 0; i < n; i++)
{
if (*s1 != '\0' && *s2 != '\0')
{
return 0;
}

if (*s1 != *s2)
{
return (int) * s1 - (int) * s2;
}
s1++;
s2++;
}

return 0;
}
43 changes: 43 additions & 0 deletions courses/cunix2/libft/src/ft_strnstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <stddef.h>

char *ft_strnstr(const char *haystack, const char *needle, size_t n)
{
if (*needle == '\0')
{
return (char *) haystack;
}

char *h_pos;
char *n_pos;
char *end_pos = (char *)(haystack + n);
int found = 0;

while (*haystack != '\0' && haystack != end_pos)
{
h_pos = (char *) haystack;
n_pos = (char *) needle;

while (*n_pos != '\0')
{
if (h_pos != end_pos && *h_pos++ == *n_pos++)
{
found = 1;
}
else
{
found = 0;
break;
}

}

if (found)
{
return (char *) haystack;
}

haystack++;
}

return 0;
}
38 changes: 38 additions & 0 deletions courses/cunix2/libft/src/ft_strsplit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

#include <stdlib.h>
#include "../libft.h"

char **ft_strsplit(char const *s, char c)
{
unsigned int num_words = ft_words(s, c);
char **words = (char **) malloc(sizeof(char *) * (num_words + 1));

unsigned int curr_word_len;
char *word = NULL;

while (*s != '\0')
{
while (*s == c && *s != '\0')
{
s++;
}

if (*s != '\0')
{
curr_word_len = ft_strclen(s, c);
word = (char *) malloc(sizeof(char) * (curr_word_len + 1));

while (*s != c && *s != '\0')
{
*word++ = *s++;
}

*word = '\0';
*words++ = word - curr_word_len;
}
}

*words = NULL;

return words - num_words;
}
44 changes: 44 additions & 0 deletions courses/cunix2/libft/src/ft_strstr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
char *ft_strstr(const char *haystack, const char *needle)
{
if (*haystack == '\0')
{
return (char *) needle;
}

if (*needle == '\0')
{
return (char *) haystack;
}

char *h_pos;
char *n_pos;
int found = 0;

while (*haystack != '\0')
{
h_pos = (char *) haystack;
n_pos = (char *) needle;

while (*n_pos != '\0')
{
if (*h_pos++ == *n_pos++)
{
found = 1;
}
else
{
found = 0;
break;
}
}

if (found)
{
return (char *) haystack;
}

haystack++;
}

return 0;
}
Loading