feat: (cstr) strcount
This commit is contained in:
parent
1f07e35af5
commit
25c5b760e0
13
lib/cstr.c
13
lib/cstr.c
|
@ -60,3 +60,16 @@ strtrimr (char *str)
|
|||
|
||||
return i;
|
||||
}
|
||||
|
||||
size_t
|
||||
strcount (const char *str, char c)
|
||||
{
|
||||
size_t count;
|
||||
|
||||
count = 0;
|
||||
while (*str)
|
||||
if (*str++ == c)
|
||||
count += 1;
|
||||
|
||||
return count;
|
||||
}
|
||||
|
|
|
@ -6,5 +6,6 @@
|
|||
char *strtrim (char *str); /* trim whitespace of a cstring (both beginning and end) */
|
||||
char *strtriml (char *str); /* trim whitespace of a cstring on the left (beginning of string). */
|
||||
size_t strtrimr (char *str); /* trim whitespace of a cstring on the right (end of string) */
|
||||
size_t strcount (const char *str, char c);
|
||||
|
||||
#endif // CUTILS_CSTR_H_
|
||||
|
|
|
@ -29,6 +29,11 @@ test_cstr (void)
|
|||
test_add (&suite, test_assert_op (sn, ==, s2 + 4), "strtrim pointer");
|
||||
test_add (&suite, test_assert_op (strlen (s2), ==, 17), "strtrim original pointer length");
|
||||
test_add (&suite, test_assert_op (strlen (sn), ==, 13), "strtrim new pointer length");
|
||||
|
||||
char *s3 = "Hello, World!\n";
|
||||
test_add (&suite, test_assert_op (strcount (s3, 'l'), ==, 3), "strcount multiple occurances");
|
||||
test_add (&suite, test_assert_op (strcount (s3, '\n'), ==, 1), "strcount one occurance");
|
||||
test_add (&suite, test_assert_op (strcount (s3, 'X'), ==, 0), "strcount no occurance");
|
||||
|
||||
return test_suite_get_results (&suite);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue