This commit is contained in:
2026-02-11 13:20:47 +01:00
commit d3645f663c
30 changed files with 403 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
CC=gcc
SRC = my_strings.c main.c
CFLAGS= -Wall -Wextra -Werror
LDFLAGS= -fsanitize=address -g
# Computes the basic main for tests
main: ${SRC}
gcc $(CFLAGS) $^ -o main -lm
#Check for memory errors/leaks and to use gdb
debug: ${SRC}
gcc $(CFLAGS) $^ -o debug $(LDFLAGS)
clean:
$(RM) main debug

View File

@@ -0,0 +1,24 @@
#include <stdio.h>
#include "my_strings.h"
int main(){
// struct string *str = my_str_init("Hello World!", 12);
// printf("data: %.*s\n", (int)str->size, str->data);
//struct string *str = my_str_init("Hello World!", 12);
//my_str_destroy(str);
//struct string *str = my_str_init("Hello Alien World!", 18);
//my_str_puts(str);
// my_str_destroy(str);
struct string *str = my_str_init("Hello ", 6);
str = my_str_n_cat(str, "Alien World!", 13);
my_str_puts(str);
my_str_destroy(str);
}

View File

@@ -0,0 +1,66 @@
#include "my_strings.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
//' -> ' est un racc pour *rslt.data
struct string *my_str_init(const char* s, size_t size){
struct string *rslt = malloc(sizeof(struct string));
if(rslt == NULL) return NULL;
if(size == 0 || s == NULL) {
rslt->data = NULL;
rslt->size = 0;
}
else{
rslt->data = malloc(size);
if(rslt->data == NULL){
free(rslt);
return NULL;
}
memcpy(rslt->data, s, size);
rslt->size = size;
}
return rslt;
}
void my_str_destroy(struct string *str){
if(str == NULL) return;
free(str->data);
free(str);
}
void my_str_puts(struct string *str){
if(str == NULL) return;
if(str->data == NULL || str->size == 0)printf("\n");
for (size_t i = 0; i < str->size; i++) putchar(*((str->data) + i));
}
struct string *my_str_n_cat(struct string *str1, const char *str2, size_t str2_len){
if (str2 == NULL || str2_len == 0) return str1;
if (str1 == NULL){
struct string *rslt = my_str_init(str2, str2_len);
return rslt;
}
str1->data = realloc(str1->data, (str1->size + str2_len) * sizeof(char));
if(str1 == NULL) return str1;
//printf("test");
int index = 0;
while (*(str2 + index)){
str1->data[str1->size + index] = str2[index];
index ++;
}
str1->size = str1->size + index;
return str1;
}

View File

@@ -0,0 +1,17 @@
#ifndef MY_STRINGS_H
#define MY_STRINGS_H
#include <strings.h>
struct string{
char *data;
size_t size;
};
struct string *my_str_init(const char* s, size_t size);
void my_str_destroy(struct string *str);
void my_str_puts(struct string *str);
struct string *my_str_n_cat(struct string *str1, const char *str2, size_t str2_len);
#endif