feat: (cstr) strupcase, strdowncase

This commit is contained in:
thetek 2023-02-12 17:00:07 +01:00
parent 25c5b760e0
commit 712baafd59
3 changed files with 31 additions and 0 deletions

View File

@ -73,3 +73,25 @@ strcount (const char *str, char c)
return count;
}
void
strdowncase (char *str)
{
while (*str)
{
if (isupper (*str))
*str += 0x20;
str++;
}
}
void
strupcase (char *str)
{
while (*str)
{
if (islower (*str))
*str -= 0x20;
str++;
}
}

View File

@ -7,5 +7,7 @@ char *strtrim (char *str); /* trim whitespace of a cstring (both beginning and e
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);
void strdowncase (char *str);
void strupcase (char *str);
#endif // CUTILS_CSTR_H_

View File

@ -34,6 +34,13 @@ test_cstr (void)
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");
char s4[] = "Hello, World!\n";
char s5[] = "Hello, World!\n";
strdowncase (s4);
strupcase (s5);
test_add (&suite, test_assert (!strcmp (s4, "hello, world!\n")), "strdowncase");
test_add (&suite, test_assert (!strcmp (s5, "HELLO, WORLD!\n")), "strupcase");
return test_suite_get_results (&suite);
}