diff options
Diffstat (limited to 'utils.c')
-rw-r--r-- | utils.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -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); +} |