Up until ex04

This commit is contained in:
tosu 2023-03-30 21:31:47 +02:00
parent cf02c2d576
commit 63562f4c1b
6 changed files with 368 additions and 65 deletions

View File

@ -1,87 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 20:53:45 by tosuman #+# #+# */
/* Updated: 2023/03/30 20:53:45 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
char *ft_strcpy(char *dest, char *src)
char *ft_strcpy(char *dest, char *src)
{
char *o_dest;
char *o_dest;
o_dest = dest;
while (*src)
*dest++ = *src++;
*dest = 0;
return (o_dest);
o_dest = dest;
while (*src)
*dest++ = *src++;
*dest = 0;
return (o_dest);
}
int ft_strlen(char *str)
int ft_strlen(char *str)
{
int size;
int size;
size = 0;
while (*str++)
++size;
return (size);
size = 0;
while (*str++)
++size;
return (size);
}
char *ft_strdup(char *src)
char *ft_strdup(char *src)
{
char *new_str;
char *new_str;
new_str = malloc(8 * ft_strlen(src));
ft_strcpy(new_str, src);
return (new_str);
new_str = malloc(8 * ft_strlen(src));
ft_strcpy(new_str, src);
return (new_str);
}
/* ////
#include <stdio.h>
#include <string.h>
int main(void)
int main(void)
{
char *str1;
char *stra;
char *strb;
char *str2;
char *str3;
char *str4;
char *str_std1;
char *str_stda;
char *str_stdb;
char *str_std2;
char *str_std3;
char *str_std4;
char *str1;
char *stra;
char *strb;
char *str2;
char *str3;
char *str4;
char *str_std1;
char *str_stda;
char *str_stdb;
char *str_std2;
char *str_std3;
char *str_std4;
printf("### Testing ft_strdup ###\n");
str1 = "The String";
stra = "The String";
strb = str1;
str2 = ft_strdup(str1);
str3 = ft_strdup(str1);
str4 = ft_strdup(str2);
printf("str1 value: %s, str1 address: %p\n", str1, str1);
printf("stra value: %s, stra address: %p (==str1)\n", stra, stra);
printf("strb value: %s, strb address: %p (==str1)\n", strb, strb);
printf("str2 value: %s, str2 address: %p (diff)\n", str2, str2);
printf("str3 value: %s, str3 address: %p (diff)\n", str3, str3);
printf("str4 value: %s, str4 address: %p (diff)\n", str4, str4);
free(str2);
free(str3);
free(str4);
printf("\n### Testing strdup ###\n");
str_std1 = "The Standard String";
str_stda = "The Standard String";
str_stdb = str_std1;
str_std2 = strdup(str_std1);
str_std3 = strdup(str_std1);
str_std4 = strdup(str_std2);
printf("str_std1 value: %s, str_std1 address: %p\n", str_std1, str_std1);
printf("str_stda value: %s, str_stda address: %p (==str_std1)\n", str_stda, str_stda);
printf("str_stdb value: %s, str_stdb address: %p (==str_std1)\n", str_stdb, str_stdb);
printf("str_std2 value: %s, str_std2 address: %p (diff)\n", str_std2, str_std2);
printf("str_std3 value: %s, str_std3 address: %p (diff)\n", str_std3, str_std3);
printf("str_std4 value: %s, str_std4 address: %p (diff)\n", str_std4, str_std4);
free(str_std2);
free(str_std3);
free(str_std4);
return (0);
printf("### Testing ft_strdup ###\n");
str1 = "The String";
stra = "The String";
strb = str1;
str2 = ft_strdup(str1);
str3 = ft_strdup(str1);
str4 = ft_strdup(str2);
printf("str1 value: %s, str1 address: %p\n", str1, str1);
printf("stra value: %s, stra address: %p (==str1)\n", stra, stra);
printf("strb value: %s, strb address: %p (==str1)\n", strb, strb);
printf("str2 value: %s, str2 address: %p (diff)\n", str2, str2);
printf("str3 value: %s, str3 address: %p (diff)\n", str3, str3);
printf("str4 value: %s, str4 address: %p (diff)\n", str4, str4);
free(str2);
free(str3);
free(str4);
printf("\n### Testing strdup ###\n");
str_std1 = "The Standard String";
str_stda = "The Standard String";
str_stdb = str_std1;
str_std2 = strdup(str_std1);
str_std3 = strdup(str_std1);
str_std4 = strdup(str_std2);
printf("str_std1 value: %s, str_std1 address: %p\n",
str_std1, str_std1);
printf("str_stda value: %s, str_stda address: %p (==str_std1)\n",
str_stda, str_stda);
printf("str_stdb value: %s, str_stdb address: %p (==str_std1)\n",
str_stdb, str_stdb);
printf("str_std2 value: %s, str_std2 address: %p (diff)\n",
str_std2, str_std2);
printf("str_std3 value: %s, str_std3 address: %p (diff)\n",
str_std3, str_std3);
printf("str_std4 value: %s, str_std4 address: %p (diff)\n",
str_std4, str_std4);
free(str_std2);
free(str_std3);
free(str_std4);
return (0);
}
*/ ////

BIN
ex00/main

Binary file not shown.

62
ex01/ft_range.c Normal file
View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 19:55:07 by tosuman #+# #+# */
/* Updated: 2023/03/30 19:55:08 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
int *ft_range(int min, int max)
{
int *range;
int idx;
if (min >= max)
return ((void *) 0);
range = malloc(sizeof(int) * (max - min));
idx = -1;
while (++idx < max - min)
range[idx] = idx + min;
return (range);
}
/* ////
#include <stdio.h>
int main(void)
{
int i;
int j;
int k;
int *arr;
k = -1;
while (++k < 5)
{
i = -1;
while (++i < 6)
{
arr = ft_range(k, i);
printf("Range from k:%d to i:%d, ptr: %p: <", k, i, arr);
j = -1;
while (++j < i - k)
{
if (j < i - k - 1)
printf("%d, ", arr[j]);
else
printf("%d", arr[j]);
}
printf(">\n");
free(arr);
}
printf("\n");
}
return (0);
}
*/ ////

68
ex02/ft_ultimate_range.c Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ultimate_range.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 20:55:05 by tosuman #+# #+# */
/* Updated: 2023/03/30 20:55:06 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int ft_ultimate_range(int **range, int min, int max)
{
int idx;
if (min >= max)
{
*range = (void *) 0;
return (-1);
}
*range = malloc(sizeof(int) * (max - min));
idx = -1;
while (++idx < max - min)
range[0][idx] = idx + min;
return (max - min);
}
/* ////
#include <stdio.h>
int main(void)
{
int idx;
int min;
int max;
int *arr;
int size;
min = -1;
while (++min < 5)
{
max = -1;
while (++max < 6)
{
size = ft_ultimate_range(&arr, min, max);
printf("Range (size: %d) from min:%d to max:%d, ptr: %p: <",
size, min, max, arr);
idx = -1;
while (++idx < max - min)
{
if (idx < max - min - 1)
printf("%d, ", arr[idx]);
else
printf("%d", arr[idx]);
}
printf(">\n");
if (arr != NULL)
free(arr);
}
printf("\n");
}
return (0);
}
*/ ////

100
ex03/ft_strjoin.c Normal file
View File

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/30 20:50:21 by tosuman #+# #+# */
/* Updated: 2023/03/30 20:50:21 by tosuman ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
int ft_strlen(char *str)
{
int size;
size = 0;
while (*str++)
++size;
return (size);
}
char *ft_strcat(char *dest, char *src)
{
char *orig_s1;
orig_s1 = dest;
while (*dest)
++dest;
while (*src)
*dest++ = *src++;
*dest = 0;
return (orig_s1);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
int total_len;
int sep_len;
int idx;
char *joined_str;
total_len = 0;
sep_len = ft_strlen(sep);
idx = -1;
while (++idx < size)
total_len += ft_strlen(strs[idx]) + sep_len;
total_len -= sep_len;
if (total_len < 1)
joined_str = malloc(sizeof(char) * 1);
else
joined_str = malloc(sizeof(char) * (total_len + 1));
idx = -1;
joined_str[0] = 0;
while (++idx < size)
{
ft_strcat(joined_str, strs[idx]);
if (idx < size - 1)
ft_strcat(joined_str, sep);
}
return (joined_str);
}
/* ////
#include <stdio.h>
int main(void)
{
char **strs;
char *sep;
int size;
char *result;
size = 5;
// sep = "<DELIM>";
sep = "";
strs = malloc(sizeof((char *) 0) * size);
strs[0] = "First";
strs[1] = "Second";
strs[2] = "Third";
strs[3] = "Fourth";
strs[4] = "Fifth";
printf("strs size: '%d'\n", size);
printf("The delim: '%s'\n", sep);
printf("strs[0]: '%s'\n", strs[0]);
printf("strs[1]: '%s'\n", strs[1]);
printf("strs[2]: '%s'\n", strs[2]);
printf("strs[3]: '%s'\n", strs[3]);
printf("strs[4]: '%s'\n", strs[4]);
result = ft_strjoin(size, strs, sep);
printf("The concatenated string: '%s'\n", result);
printf("Length of that string: %d\n", ft_strlen(result));
free(result);
free(strs);
return (0);
}
*/ ////

56
ex04/ft_convert_base.c Normal file
View File

@ -0,0 +1,56 @@
char *ft_convert_base(char *nbr, char *base_from, char *base_to)
{
}
#include <stdio.h>
#define INVALID_FROM_BASE_1 "01234567890"
#define INVALID_TO_BASE_1 "010"
#define INVALID_FROM_BASE_2 "010"
#define INVALID_TO_BASE_2 "01234567890"
#define FROM_BASE_1 "0123456789"
#define TO_BASE_1 "0123456789"
#define FROM_BASE_2 "0123456789"
#define TO_BASE_2 "01"
#define FROM_BASE_2 "01"
#define TO_BASE_2 "0123456789"
#define FROM_BASE_3 "0123456789"
#define TO_BASE_3 "0123456789abcdef"
#define FROM_BASE_3 "01"
#define TO_BASE_3 "0123456789abcdef"
#define FROM_BASE_4 "0123456789abcdef"
#define TO_BASE_4 "01"
int main(void)
{
char *from_base;
char *to_base;
char *nbr;
from_base = FROM_BASE_2;
to_base = TO_BASE_2;
printf("FROM BASE: %s, TO BASE: %s\n", from_base, to_base);
// nbr = "-2"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
// nbr = "-1"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "0"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "1"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "2"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "3"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "4"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "5"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "6"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "7"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "8"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "9"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
nbr = "10"; printf("Input: <%s>, Output: <%s>\n", nbr, ft_convert_base(nbr, from_base, to_base));
printf("\n");
return (0);
}