Algorithms
Strings
Include strdup() properly.
Remove unneeded index variable (passing intergral types as 'const' is just plan bizarre).
#ifdef __STDC_ALLOC_LIB__ #define __STDC_WANT_LIB_EXT2__ 1 #else #define _POSIX_C_SOURCE 200809L #endif #include <stdlib.h> #include <string.h> /* Heap allocate the string and return it. */ char* remove_string(const char *input, int n) { if (n <= 0 || input == NULL) return NULL; size_t len = 0; while (n--) { input += len; len = strlen(input); if (!len) return NULL; len++; } return strdup(input); }
- #ifdef __STDC_ALLOC_LIB__
- #define __STDC_WANT_LIB_EXT2__ 1
- #else
- #define _POSIX_C_SOURCE 200809L
- #endif
- #include <stdlib.h>
- #include <string.h>
- /*
- Heap allocate the string and return it.
- */
char* remove_string(const char *input, const int n){if (n <= 0 || input == NULL) return NULL;for (size_t i = 0, len = 0; i < (size_t)n; i++){- char* remove_string(const char *input, int n) {
- if (n <= 0 || input == NULL)
- return NULL;
- size_t len = 0;
- while (n--) {
- input += len;
if ((len = strlen(input) + 1) == 1) return NULL;- len = strlen(input);
- if (!len)
- return NULL;
- len++;
- }
- return strdup(input);
- }
#include <criterion/criterion.h> #include <stdlib.h> #include <string.h> char* remove_string(const char *input, const int n); void tester(const char *input, size_t index, const char *exp) { char *result = remove_string(input, index); if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result); else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result); free(result); } Test(remove_string, should_pass_all_the_tests_provided) { tester("hello\0world\0", 1, "hello"); tester("well\0done\0", 2, "done"); tester("you\0are\0close\0", 3, "close"); tester("you\0almost\0finished\0the\0kumite\0", 5, "kumite"); tester("not\0the\0last\0string\0", 3, "last"); tester("edge\0case\0", 0, NULL); tester("another\0edge\0case\0", 4, NULL); tester("\0", 0, NULL); tester("\0", 1, NULL); tester(NULL, 1, NULL); tester("\0Wow\0", 0, NULL); tester("\0Wow\0", -1, NULL); tester("one\0two\0three\0four", 4, "four"); tester("one\0two\0three\0\0four", 4, NULL); }
- #include <criterion/criterion.h>
- #include <stdlib.h>
- #include <string.h>
- char* remove_string(const char *input, const int n);
- void tester(const char *input, size_t index, const char *exp)
- {
- char *result = remove_string(input, index);
- if (exp == NULL) cr_assert_eq(result, NULL, "Expected NULL but got %s.", result);
- else cr_assert_str_eq(result, exp, "Expected '%s' but got '%s'.", exp, result);
- free(result);
- }
- Test(remove_string, should_pass_all_the_tests_provided)
- {
- tester("hello\0world\0", 1, "hello");
- tester("well\0done\0", 2, "done");
- tester("you\0are\0close\0", 3, "close");
- tester("you\0almost\0finished\0the\0kumite\0", 5, "kumite");
- tester("not\0the\0last\0string\0", 3, "last");
- tester("edge\0case\0", 0, NULL);
- tester("another\0edge\0case\0", 4, NULL);
- tester("\0", 0, NULL);
- tester("\0", 1, NULL);
- tester(NULL, 1, NULL);
- tester("\0Wow\0", 0, NULL);
- tester("\0Wow\0", -1, NULL);
- tester("one\0two\0three\0four", 4, "four");
- tester("one\0two\0three\0\0four", 4, NULL);
- }