summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
authorJasper2025-08-31 15:47:31 +0200
committerJasper2025-08-31 15:47:31 +0200
commit7d763b5778cfbf81ee420558d13e8acbb66d860a (patch)
treee67fa668944e79526cbdf7b6e8eed6e8d81ebdb6 /utils.c
Initial commit
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c39
1 files changed, 39 insertions, 0 deletions
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 <string.h>
+#include <assert.h>
+#include <stdlib.h>
+
+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);
+}