diff --git a/c02/common.c b/c02/common.c new file mode 100644 index 0000000..c1c70e3 --- /dev/null +++ b/c02/common.c @@ -0,0 +1,38 @@ +#include +#include +#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); +} diff --git a/c02/ex00.c b/c02/ex00.c index 248dee6..8290f98 100644 --- a/c02/ex00.c +++ b/c02/ex00.c @@ -1,9 +1,25 @@ +#include "common.c" char *ft_strcpy(char *dest, char *src); -int main(void) +int main(void) { - char s[100] = "hello"; - char d[100] = "goodbye"; - - ft_strcpy(s, d); + char s1[100] = TEST_SOURCE; + char d1[100] = TEST_DESTINATION; + char s2[100] = TEST_SOURCE; + 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); }