From 7d763b5778cfbf81ee420558d13e8acbb66d860a Mon Sep 17 00:00:00 2001 From: Jasper Date: Sun, 31 Aug 2025 15:47:31 +0200 Subject: Initial commit --- utils.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 utils.c (limited to 'utils.c') diff --git a/utils.c b/utils.c new file mode 100644 index 0000000..dff3b3f --- /dev/null +++ b/utils.c @@ -0,0 +1,39 @@ +#include +#include +#include + +char *str_delete_at(const char *str, const int pos) +{ + char *tmp = malloc(sizeof(char) * (strlen(str) + 1)); + assert(tmp != NULL); + strcpy(tmp, str); + + int len = strlen(str); + if (pos >= len) + return tmp; + + for (int i = 0; i < len; ++i) + { + if (i >= pos) + tmp[i] = tmp[i + 1]; + } + + return tmp; +} + +size_t str_count_occ(const char *str, const char ch) +{ + size_t count = 0; + for (int i = 0; str[i] != '\0'; ++i) + { + if (str[i] == ch) + count += 1; + } + return count; +} + +/* Return random integer between lower bound bound_l (inclusive) and upper bound bound_u (exclusive) */ +int int_rand(const int bound_l, const int bound_u) +{ + return bound_l + (rand() % bound_u); +} -- cgit v1.2.3