commit ea6b22e51c0f7ada855ddfb41adc3e19e5582aeb Author: trmartinek Date: Wed Dec 15 16:25:30 2021 +0100 first commit of myLibC diff --git a/find_next_prime.c b/find_next_prime.c new file mode 100644 index 0000000..68026a3 --- /dev/null +++ b/find_next_prime.c @@ -0,0 +1,22 @@ +int is_prime(int nb) +{ + unsigned int i; + + i = 2; + while (i <= nb / i) + { + if (nb % i == 0) + return (1); + i++; + } + return (0); +} + +int find_next_prime(int nb) +{ + if (nb <= 2) + return (2); + while (!(is_prime(nb) == 0)) + nb++; + return (nb); +} diff --git a/my_atoi.c b/my_atoi.c new file mode 100644 index 0000000..6beea4b --- /dev/null +++ b/my_atoi.c @@ -0,0 +1,25 @@ +int my_atoi(char *str) +{ + int i; + int posi; + int num; + + i = 0; + posi = 1; + num = 0; + while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13)) + i++; + while (str[i] == '-' || str[i] == '+') + { + if (str[i] == '-') + posi = posi * -1; + i++; + } + while (str[i] >= 48 && str[i] <= 57) + { + num = num * 10; + num = num + (str[i] - 48); + i++; + } + return (num * posi); +} diff --git a/my_file.c b/my_file.c new file mode 100644 index 0000000..c7fdf45 --- /dev/null +++ b/my_file.c @@ -0,0 +1,101 @@ +#include +#define TYPE int + +typedef struct maillon +{ + TYPE v; + struct maillon *suiv; +} Maillon, *File; + +File filenouv(void) +{ + return NULL; +} + +bool vide(File f) +{ + return f==NULL; +} + +TYPE tete(File f) +{ + if(vide(f)) + { + printf("OpĂration interdite\n"); + exit (1); + } + return f->suiv->v; +} + +int longueur(File f) +{ + if(vide(f)) + { + printf("OpĂration interdite\n"); + exit (1); + } + Maillon *m; + int i; + i = 1; + m = f->suiv; + while(m != f) + { + ++i + m = m->suiv; + } + return i; +} + +/*void afficher(File f) +{ + if(vide(f)) + return; + Maillon *m; + m = f->suiv; + while(m != f) + { + printf("%d\t", m->v); + m = m->suiv; + } + printf("\n"); +}*/ + +File supprTete(File f) +{ + if(vide(f)) + { + printf("OpĂration interdite\n"); + exit (1); + } + if(f == f->suiv) + { + free(f); + return NULL; + } + Maillon *tmp; + tmp = f->suiv; + tmp->suiv = tmp->suiv->suiv; + free(f); + return tmp; +} + +File addQueue(File f, TYPE x) +{ + Maillon *m; + if(!m = (Maillon *)malloc(sizeof(Maillon))) + { + printf("ProblĂme Malloc\n"); + exit (1); + } + m->v = x; + if(vide(f)) + { + f = m; + m->suiv = m; + return f; + } + m->suiv = f->suiv; + f->suiv = m; + f = m; + return f; +} diff --git a/my_is_prime.c b/my_is_prime.c new file mode 100644 index 0000000..afe6711 --- /dev/null +++ b/my_is_prime.c @@ -0,0 +1,15 @@ +int my_is_prime(int nb) +{ + int i; + + i = 2; + if (nb < 2) + return (0); + while (i <= nb / i) + { + if (nb % i == 0) + return (0); + i++; + } + return (1); +} diff --git a/my_split.c b/my_split.c new file mode 100644 index 0000000..70b577e --- /dev/null +++ b/my_split.c @@ -0,0 +1,84 @@ +#include + +int char_is_separator(char c, char *charset) +{ + int i; + + i = 0; + while (charset[i] != '\0') + { + if (c == charset[i]) + return (1); + i++; + } + if (c == '\0') + return (1); + return (0); +} + +int count_words(char *str, char *charset) +{ + int i; + int words; + + words = 0; + i = 0; + while (str[i] != '\0') + { + if (char_is_separator(str[i + 1], charset) == 1 + && char_is_separator(str[i], charset) == 0) + words++; + i++; + } + return (words); +} + +void write_word(char *dest, char *from, char *charset) +{ + int i; + + i = 0; + while (char_is_separator(from[i], charset) == 0) + { + dest[i] = from[i]; + i++; + } + dest[i] = '\0'; +} + +void write_split(char **split, char *str, char *charset) +{ + int i; + int j; + int word; + + word = 0; + i = 0; + while (str[i] != '\0') + { + if (char_is_separator(str[i], charset) == 1) + i++; + else + { + j = 0; + while (char_is_separator(str[i + j], charset) == 0) + j++; + split[word] = (char*)malloc(sizeof(char) * (j + 1)); + write_word(split[word], str + i, charset); + i += j; + word++; + } + } +} + +char **my_split(char *str, char *charset) +{ + char **split; + int words; + + words = count_words(str, charset); + split = (char**)malloc(sizeof(char*) * (words + 1)); + split[words] = 0; + write_split(split, str, charset); + return (split); +} diff --git a/my_sqrt.c b/my_sqrt.c new file mode 100644 index 0000000..792dd31 --- /dev/null +++ b/my_sqrt.c @@ -0,0 +1,15 @@ +int sqrt(int nb) +{ + int i; + + i = 0; + if (nb < 0) + return (0); + while (i < 46341 || i < nb) + { + if (i * i == nb) + return (i); + i++; + } + return (0); +} diff --git a/my_strcat.c b/my_strcat.c new file mode 100644 index 0000000..44bbf4e --- /dev/null +++ b/my_strcat.c @@ -0,0 +1,18 @@ +char *my_strcat(char *dest, char *src) +{ + int i; + int j; + + i = 0; + j = 0; + while (dest[i] != '\0') + i++; + while (src[j] != '\0') + { + dest[i] = src[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/my_strcmp.c b/my_strcmp.c new file mode 100644 index 0000000..6f3518b --- /dev/null +++ b/my_strcmp.c @@ -0,0 +1,13 @@ +int my_strcmp(char *s1, char *s2) +{ + int i; + + i = 0; + while (s1[i]) + { + if (s1[i] != s2[i]) + return (s1[i] - s2[i]); + i++; + } + return (s1[i] - s2[i]); +} diff --git a/my_strcpy.c b/my_strcpy.c new file mode 100644 index 0000000..cf6cdaf --- /dev/null +++ b/my_strcpy.c @@ -0,0 +1,13 @@ +char *my_strcpy(char *dest, char *src) +{ + int i; + + i = 0; + while (src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/my_strdup.c b/my_strdup.c new file mode 100644 index 0000000..334d221 --- /dev/null +++ b/my_strdup.c @@ -0,0 +1,23 @@ +#include + +char *my_strdup(char *src) +{ + int size; + char *new; + int i; + + size = 0; + i = 0; + while (src[size]) + size++; + new = (char*)malloc(sizeof(char) * (size + 1)); + if (!new) + return (NULL); + while (src[i]) + { + new[i] = src[i]; + i++; + } + new[i] = '\0'; + return (new); +} diff --git a/my_strjoin.c b/my_strjoin.c new file mode 100644 index 0000000..044aadc --- /dev/null +++ b/my_strjoin.c @@ -0,0 +1,82 @@ +#include + +int sep_size(char *str) +{ + int i; + + i = 0; + while (str[i]) + i++; + return (i); +} + +int full_size(int size, char **strs, char *sep) +{ + int i; + int j; + int len; + + len = 0; + i = 0; + j = 0; + while (i < size) + { + j = 0; + while (strs[i][j]) + { + len++; + j++; + } + if (i + 1 < size) + len += sep_size(sep); + i++; + } + return (len); +} + +void write_str(int size, char **strs, char *sep, char *str) +{ + int i; + int j; + int k; + + i = -1; + j = 0; + k = 0; + while (++i < size) + { + j = -1; + while (strs[i][++j]) + { + str[k] = strs[i][j]; + k++; + } + j = -1; + while (i + 1 < size && sep[++j]) + { + str[k] = sep[j]; + k++; + } + } + str[k] = '\0'; +} + +char *my_strjoin(int size, char **strs, char *sep) +{ + char *str; + + if (size > 0) + { + if (!(str = (char*) + malloc(sizeof(char) * (full_size(size, strs, sep) + 1)))) + return (NULL); + write_str(size, strs, sep, str); + } + else + { + if (!(str = (char*)malloc(sizeof(char) * 1))) + return (NULL); + str[0] = '\0'; + } + return (str); +} diff --git a/my_strlen.c b/my_strlen.c new file mode 100644 index 0000000..02f10d8 --- /dev/null +++ b/my_strlen.c @@ -0,0 +1,9 @@ +int my_strlen(char *str) +{ + int len; + + len = 0; + while (str[len] != '\0') + len++; + return (len); +} diff --git a/my_strncat.c b/my_strncat.c new file mode 100644 index 0000000..1960752 --- /dev/null +++ b/my_strncat.c @@ -0,0 +1,18 @@ +char *my_strncat(char *dest, char *src, unsigned int nb) +{ + unsigned int i; + unsigned int j; + + i = 0; + j = 0; + while (dest[i] != '\0') + i++; + while (src[j] != '\0' && j < nb) + { + dest[i] = src[j]; + i++; + j++; + } + dest[i] = '\0'; + return (dest); +} diff --git a/my_strncmp.c b/my_strncmp.c new file mode 100644 index 0000000..5325419 --- /dev/null +++ b/my_strncmp.c @@ -0,0 +1,11 @@ +int my_strncmp(char *s1, char *s2, unsigned int n) +{ + unsigned int i; + + i = 0; + if (n == 0) + return (0); + while (s1[i] == s2[i] && s1[i] != '\0' && i < n - 1) + i++; + return (s1[i] - s2[i]); +} diff --git a/my_strncpy.c b/my_strncpy.c new file mode 100644 index 0000000..a51db8f --- /dev/null +++ b/my_strncpy.c @@ -0,0 +1,17 @@ +char *my_strncpy(char *dest, char *src, unsigned int n) +{ + unsigned int i; + + i = 0; + while (i < n && src[i] != '\0') + { + dest[i] = src[i]; + i++; + } + while (i < n) + { + dest[i] = '\0'; + i++; + } + return (dest); +} diff --git a/my_strstr.c b/my_strstr.c new file mode 100644 index 0000000..f05b9c7 --- /dev/null +++ b/my_strstr.c @@ -0,0 +1,25 @@ +char *strstr(char *str, char *to_find) +{ + int i; + int j; + + i = 0; + j = 0; + if (!(to_find[j])) + return (str); + while (str[i]) + { + if (str[i] == to_find[0]) + { + j = 0; + while (str[i + j] && str[i + j] == to_find[j]) + j++; + if (to_find[j] == '\0') + return (&str[i]); + } + if (to_find[j] == '\0') + return (&str[i]); + i++; + } + return (0); +} diff --git a/my_ultimate_div_mod.c b/my_ultimate_div_mod.c new file mode 100644 index 0000000..0ac28fb --- /dev/null +++ b/my_ultimate_div_mod.c @@ -0,0 +1,10 @@ +void ultimate_div_mod(int *a, int *b) +{ + int buf_a; + int buf_b; + + buf_a = *a; + buf_b = *b; + *a = buf_a / buf_b; + *b = buf_a % buf_b; +}