add c02/ex00

This commit is contained in:
Aleksei Chubukov 2023-11-23 22:26:57 +04:00
parent b34fd5e7e2
commit 92b42aa8ec
2 changed files with 59 additions and 5 deletions

38
c02/common.c Normal file
View File

@ -0,0 +1,38 @@
#include <string.h>
#include <stdio.h>
#define TEST_SOURCE "HeLlo world!\0It's a\tBEAUTIFUL\tday!"
#define TEST_SOURCE_LENGHT 35
#define TEST_DESTINATION "GooDBye\0and thanks \tfor\rall the fish!"
#define TEST_DESTINATION_LENGHT 38
#define FG_WHT "\x1B[37m"
#define FG_MAG "\x1B[35m"
#define COLOR_RESET "\x1B[0m"
void dump(char *data, int len)
{
int i = 0;
while (i < len)
{
if (data[i] >= ' ' && data[i] < 127)
printf(" %c ", data[i]);
else
printf(FG_MAG "\\%02u" COLOR_RESET, data[i]);
i++;
}
printf("\n");
}
int bufcmp(char *this, char *other, int len)
{
int i = 0;
while (i < len)
{
if (this[i] != other[i])
return (i + 1);
i++;
}
return (0);
}

View File

@ -1,9 +1,25 @@
#include "common.c"
char *ft_strcpy(char *dest, char *src); char *ft_strcpy(char *dest, char *src);
int main(void) int main(void)
{ {
char s[100] = "hello"; char s1[100] = TEST_SOURCE;
char d[100] = "goodbye"; char d1[100] = TEST_DESTINATION;
char s2[100] = TEST_SOURCE;
ft_strcpy(s, d); char d2[100] = TEST_DESTINATION;
char* r1, *r2;
printf("initial strings:\n");
dump(s1, TEST_SOURCE_LENGHT);
dump(d1, TEST_DESTINATION_LENGHT);
r1 = strcpy(d1 ,s1);
printf("strcpy:\n");
dump(s1, TEST_SOURCE_LENGHT);
dump(d1, TEST_DESTINATION_LENGHT);
r2 = ft_strcpy(d2, s2);
printf("ft_strcpy:\n");
dump(s2, TEST_SOURCE_LENGHT);
dump(d2, TEST_DESTINATION_LENGHT);
printf("buffers comparison: s:%i d:%i\n", bufcmp(s1, s2, TEST_SOURCE_LENGHT), bufcmp(d1, d2, TEST_DESTINATION_LENGHT));
printf("return values comparison: %li %li", d1 - r1, d2 - r2);
} }