Skip to content

Commit

Permalink
C Piscine C 07 - OK 15% (2019-12-12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanda Puchille pinha committed Dec 13, 2019
1 parent cdc3d20 commit e4aa77b
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 0 deletions.
45 changes: 45 additions & 0 deletions c_piscine_c_07/ex00/ft_strdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 16:57:53 by apuchill #+# #+# */
/* Updated: 2019/12/12 17:45:03 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>

int ft_strlen(char *str)
{
int c;

c = 0;
while (str[c] != '\0')
c++;
return (c);
}

char *ft_strcpy(char *dest, char *src)
{
int c;

c = 0;
while (src[c] != '\0')
{
dest[c] = src[c];
c++;
}
dest[c] = '\0';
return (dest);
}

char *ft_strdup(char *src)
{
char *dest;

dest = malloc((ft_strlen(src) + 1) * sizeof(char));
return (dest ? ft_strcpy(dest, src) : dest);
}
Binary file added c_piscine_c_07/ex00/t_a.out
Binary file not shown.
16 changes: 16 additions & 0 deletions c_piscine_c_07/ex00/t_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <stdio.h>
#include <stdlib.h>

char *ft_strdup(char *src);

int main(void)
{
char *test;
char *dup;

test = "Testando a função strdup!";
printf("test = %s\n", test);
dup = ft_strdup(test);
printf("dup = %s\n", dup);
free(dup);
}
33 changes: 33 additions & 0 deletions c_piscine_c_07/ex01/ft_range.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 17:41:37 by apuchill #+# #+# */
/* Updated: 2019/12/12 20:52:00 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>

int *ft_range(int min, int max)
{
int *p;
int i;

if (min >= max)
return (0);
p = malloc(max - min);
if (p == NULL)
return (0);
i = 0;
while (min < max)
{
p[i] = min;
i++;
min++;
}
return (p);
}
Binary file added c_piscine_c_07/ex01/t_a.out
Binary file not shown.
45 changes: 45 additions & 0 deletions c_piscine_c_07/ex01/t_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>

int *ft_range(int min, int max);
void print_test(int *test, int size);

int main(void)
{
int *test0;
int *test1;
int *test2;
int *test3;
int *test4;

test0 = ft_range(1, 11);
printf("\n* test0 (1, 11): ");
print_test(test0, 10);

test1 = ft_range(2, 1);
printf("\n* test1 (2, 1): ");
print_test(test1, 0);

test2 = ft_range(3, 3);
printf("\n* test2 (3, 3): ");
print_test(test2, 0);

test3 = ft_range(4, 41);
printf("\n* test4 (4, 41): ");
print_test(test3, 37);

test4 = ft_range(10, -10);
printf("\n* test4 (10, -10): ");
print_test(test4, 0);
}

void print_test(int *test, int size)
{
int i;

for (i = 0; i < size - 1; i++)
{
printf("%d - ", test[i]);
}
if (size > 0)
printf("%d\n", test[size - 1]);
}
41 changes: 41 additions & 0 deletions c_piscine_c_07/ex02/ft_ultimate_range.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 20:23:51 by apuchill #+# #+# */
/* Updated: 2019/12/12 20:44:01 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>

int *ft_range(int min, int max)
{
int *p;
int i;

if (min >= max)
return (0);
p = malloc(max - min);
if (p == NULL)
return (0);
i = 0;
while (min < max)
{
p[i] = min;
i++;
min++;
}
return (p);
}

int ft_ultimate_range(int **range, int min, int max)
{
*range = ft_range(min, max);
if (*range == NULL)
return (0);
return (max - min);
}
Binary file added c_piscine_c_07/ex02/t_a.out
Binary file not shown.
53 changes: 53 additions & 0 deletions c_piscine_c_07/ex02/t_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* t_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/09 15:51:46 by ecerquei #+# #+# */
/* Updated: 2019/12/12 20:31:40 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>
#include <stdlib.h>

int ft_ultimate_range(int **range, int min, int max);
void test_ultimate_range_function(int min, int max);
void print_test(int *test, int size);

int main(void)
{
test_ultimate_range_function(3, 5);
test_ultimate_range_function(0, 11);
test_ultimate_range_function(8, 2);
test_ultimate_range_function(2, 2);
test_ultimate_range_function(10, 20);
test_ultimate_range_function(20, 15);
test_ultimate_range_function(99, 99);
test_ultimate_range_function(1, 12);
}

void test_ultimate_range_function(int min, int max)
{
int size;
int *test;

size = ft_ultimate_range(&test, min, max);
printf("\nsize = %d\n", size);
print_test(test, size);
free(test);
}

void print_test(int *test, int size)
{
int i;

for (i = 0; i < size - 1; i++)
{
printf("%d - ", test[i]);
}
if (size > 0)
printf("%d\n", test[size - 1]);
}
41 changes: 41 additions & 0 deletions c_piscine_c_07/ex03/ft_strjoin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/12 20:46:20 by apuchill #+# #+# */
/* Updated: 2019/12/12 21:27:52 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdlib.h>

char *ft_strjoin(int size, char **strs, char *sep)
{
char *s;
int i;
int j;
int c;

s = malloc(sizeof(strs));
i = 0;
c = 0;
while (i < size)
{
j = 0;
while (strs[i][j] != '\0')
{
s[c++] = strs[i][j++];
}
j = 0;
while (sep[j] != '\0' && i != size - 1)
{
s[c++] = sep[j++];
}
i++;
}
s[c] = '\0';
return (s);
}
Binary file added c_piscine_c_07/ex03/t_a.out
Binary file not shown.
33 changes: 33 additions & 0 deletions c_piscine_c_07/ex03/t_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ecerquei <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/12/09 18:19:21 by ecerquei #+# #+# */
/* Updated: 2019/12/09 18:49:42 by ecerquei ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>

char *ft_strjoin(int size, char **strs, char *sep);

int main(void)
{
char *text[10];

text[0] = "Testando";
text[1] = "a";
text[2] = "funcao";
text[3] = "ft_strjoin";
text[4] = "com";
text[5] = "o";
text[6] = "separador";
text[7] = "\' - \'";
text[8] = "e tamanho";
text[9] = "10";

printf("%s\n", ft_strjoin(10, text, " - "));
}

0 comments on commit e4aa77b

Please sign in to comment.